SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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%

    Trade 1000 units of Gold
    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">
    <b>Trade:</b><br>
    <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">
    <input type="submit" value="Trade" 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. #2
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
        <form name="market" action="http://eternalkingdoms.com/process_binfo.php?id=market" method="post">
          <b>Trade:</b><br>
         <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">
         <input type="submit" value="Trade" 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!
    Codyrobert.com - Designer and Developer

  3. #3
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, well at least this time something happens when you click "Calculate", but it just says "NaN".

  4. #4
    Romans 12:2 codyrockx's Avatar
    Join Date
    Jul 2003
    Location
    Newberg, Oregon
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there we go all fixed up
    Codyrobert.com - Designer and Developer

  5. #5
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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">
    	  <b>Trade:</b><br>
    	 <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">
    	 <input type="submit" value="Trade" 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>


Bookmarks

Posting Permissions

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