# Thread: Calculating fields in javascript

1. ## Calculating fields in javascript

Okay, I am making a market feature for my new game. I want users to be able to choose one of the 4 different resources to trade for another resource. The resources have different, changing conversion rates. I want them to be able to selectct what resource to trade and input how much to trade of it. Then to select (from a dropdown list) what resource to trade for and then click the calculate button and it will show them how much they will get of the second resource.

Example:

Gold: 0.75%
Wood: 1.38%
Food: 1.22%
Stone: 0.99%

Get ____ units of food.

So the javascript would need to be able to take 1.22 - .75, then subtract that from 1, then multiply that by 1000 (the specified amount of gold) and show that number.

Here was my attempt at it:
HTML Code:
```<form name="market" action="http://eternalkingdoms.com/process_binfo.php?id=market" method="post">
<select name="resource_1">
<option value="gold"  onclick="setVal_1(<?=\$gold?>)">Gold</option>
<option value="wood"  onclick="setVal_1(<?=\$wood?>)">Wood</option>
<option value="food"  onclick="setVal_1(<?=\$food?>)">Food</option>
<option value="stone"  onclick="setVal_1(<?=\$stone?>)">Stone</option>
</select>
<input type="text" size="11" maxlength="11" name="amount_1" class="forms" onkeyup="return addem(this.form)"><font size="1">(amount)</font>
<br>
<b>For:</b><br>
<select name="resource_2">
<option value="gold"  onclick="setVal_2(<?=\$gold?>)">Gold</option>
<option value="wood"  onclick="setVal_2(<?=\$wood?>)">Wood</option>
<option value="food"  onclick="setVal_2(<?=\$food?>)">Food</option>
<option value="stone"  onclick="setVal_2(<?=\$stone?>)">Stone</option>
</select>
<input type="text" size="11" class="forms" readonly><font size="1">(amount)</font><br>
<input type="button" onclick="calcIt()" value="Calculate" class="forms">
</form>
<script>
var _val_1 = <?=\$gold?>;
function setVal_1(_a_1) {_val_1=_a_1;}
var _val_2 = <?=\$gold?>;
function setVal_2(_a_2) {_val_2=_a_2;}
function calcIt() {
_sub = _val_2-_val_1;
_val = 1-_sub;
_v = parseInt(document.market.amount_1.value);
document.market.amount_2.value=_v*_val;
}
</script>```

2. HTML Code:
```    <form name="market" action="http://eternalkingdoms.com/process_binfo.php?id=market" method="post">
<select name="resource_1" onclick="setVal_1(this.selectedIndex.value);">
<option value="gold">Gold</option>
<option value="wood">Wood</option>
<option value="food">Food</option>
<option value="stone">Stone</option>
</select>
<input type="text" size="11" maxlength="11" name="amount_1" class="forms"><font size="1">(amount)</font>
<br>
<b>For:</b><br>
<select name="resource_2" onclick="setVal_1(this.selectedIndex.value);">
<option value="gold">Gold</option>
<option value="wood">Wood</option>
<option value="food">Food</option>
<option value="stone">Stone</option>
</select>
<input type="text" name="amount_2" size="11" class="forms" readonly><font size="1">(amount)</font><br>
<input type="button" onclick="calcIt()" value="Calculate" class="forms">
</form>
<script type="text/javascript" language="javascript">
materials = new Array();
materials["gold"] = <?=\$gold; ?>;
materials["wood"] = <?=\$wood; ?>;
materials["food"] = <?=\$food; ?>;
materials["stone"] = <?=\$stone; ?>;

var _val_1 = materials["gold"];
function setVal_1(_a_1) {
_val_1 = materials[_a_1];
}

var _val_2 = materials["gold"];
function setVal_2(_a_2) {
_val_2 = materials[_a_2];
}

function calcIt() {
_sub = _val_2-_val_1;
_val = 1-_sub;

_v = parseInt(document.forms["market"].elements["amount_1"].value);
document.forms["market"].elements["amount_2"].value = _v*_val;
}
</script>```
should work not tested tho!

3. Okay, well at least this time something happens when you click "Calculate", but it just says "NaN".

4. there we go all fixed up

5. Okay, thank you codyrockx. He helped resolve it for me over instant messenger. Final code:

HTML Code:
```<form name="market" action="http://eternalkingdoms.com/process_binfo.php?id=market" method="post">
<select name="resource_1" onclick="setVal_1(this.options[this.selectedIndex].value);">
<option value="gold">Gold</option>
<option value="wood">Wood</option>
<option value="food">Food</option>
<option value="stone">Stone</option>
</select>
<input type="text" size="11" maxlength="11" name="amount_1" class="forms"><font size="1">(amount)</font>
<br>
<b>For:</b><br>
<select name="resource_2" onclick="setVal_2(this.options[this.selectedIndex].value);">
<option value="gold">Gold</option>
<option value="wood">Wood</option>
<option value="food">Food</option>
<option value="stone">Stone</option>
</select>
<input type="text" name="amount_2" size="11" class="forms" readonly><font size="1">(amount)</font><br>
<input type="button" onclick="calcIt()" value="Calculate" class="forms">
</form>
<script type="text/javascript" language="javascript">
materials = new Array();
materials["gold"] = <?=\$gold?>;
materials["wood"] = <?=\$wood?>;
materials["food"] = <?=\$food?>;
materials["stone"] = <?=\$stone?>;

var _val_1 = materials["gold"];
function setVal_1(_a_1) {
_val_1 = materials[_a_1];
}

var _val_2 = materials["gold"];
function setVal_2(_a_2) {
_val_2 = materials[_a_2];
}

function calcIt() {
_sub = _val_2-_val_1;
_val = 1-_sub;

_v = parseInt(document.forms["market"].elements["amount_1"].value);
document.forms["market"].elements["amount_2"].value = _v*_val;
}
</script>```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•