1

I'm trying to create a simple javascript calculation script, but for some reason can't get it work.

  function calculate() {
    var USERINPUT1 = document.TESTING.INPUT1.value,
      RESULT = USERINPUT1 + (USERINPUT1 * .05);
    document.TESTING.OUTPUT1.value = RESULT;
  }
<form name="TESTING">
  <table border="1" width="600" height="200" cellpadding="10" cellspacing="3">
    <tr>
      <th colspan="2">
        <h1>5% TAX CALCULATOR</h1>
      </th>
    </tr>
    <tr>
      <th>
        <h3>ORIGINAL PRICE</h3>
      </th>
      <th>
        <h3>ORIGINAL PRICE + 5% TAX</h3>
      </th>
    </tr>
    <tr>
      <td>
        <input type="number" name="INPUT1" id="input" onchange="calculate();" />
      </td>
      <td>
        <input type="number" name="OUTPUT1" id="output">
      </td>
    </tr>
  </table>
</form>

When I enter 100 into INPUT, the OUTPUT becomes 1005, but it should be 105.

Could you please point out where the mistake is? Thank you!

0

1 Answer 1

2

document.TESTING.INPUT1.value returns string. You need to convert into number so as to perform mathematical operations.

Hence, update from

var USERINPUT1 = document.TESTING.INPUT1.value,

to

 var USERINPUT1 = parseFloat(document.TESTING.INPUT1.value),

For reference - http://plnkr.co/edit/kRxK8Kf4fO6N8E9v9D2W?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.