# how to make JavaScript calculate this correctly?

+ Post a reply

2 posts Page 1 of 1

## how to make JavaScript calculate this correctly?

by DDD » Sun Mar 12, 2017 3:20 pm

i wrote some JavaScript, but i can´t get it to calculate it right.
try to load the code below in your browser.
In the first 3 fields enter for example the numbers "20", "50" "15" and push the button.
The JavaScript is supposed to take the value from the first field and add the value from the second field and minus the value from the third field and then put the total in the fourth field.
Now it should show the result "55" in the last field (if your entered "20", "50", "15" in the 3 fields), becouse 20+50-15=55 but instead it shows 2035 (properly becouse 20+(50-15) gives 2035.
I tried also to change the "calc" variable to:
calc = income1 + income2 - expenses
calc = (income1 + income2) - expenses
calc = income1 + (income2 - expenses)
but nothing helped.
The strange thing is that when I try to do 20+50-15 in alert() it calculates it correct.

<!DOCTYPE html>
<html>
<script language="JavaScript" type="text/javascript">
function myfunction() {
income1 = document.getElementById("income1").value
income2 = document.getElementById("income2").value
expenses = document.getElementById("expenses").value
calc = income1 + income2 - expenses
document.getElementById("total").value = calc

alert(20 + 50 - 15)
}
</script>
<body>
<form>
Income: <input type="Text" id="income1"><br>
Other income: <input type="Text" id="income2"><br>
Expenses: <input type="Text" id="expenses"><br>
Total: <input type="Text" value="total" id="total"><br>
<input type="button" value="Calculate" onClick="myfunction()">
</form>
</body>
</html> DDD

• Posts: 2
• Joined: Sat May 18, 2013 12:28 pm

## Re: how to make JavaScript calculate this correctly?

by JMRKER » Thu May 18, 2017 3:22 am

The '+' character does addition of numbers AND the concatenation of strings.
The format of your input element values are strings until converted to numbers
One method of conversion is to multiply by 1.
See this correction to your code.
Note: There are other conversion functions like Number(), parseInt(), parseFloat(), etc.
More code might be necessary to avoid letting using enter alpha-characters.

Code: Select all
`<!DOCTYPE html><html><head><script language="JavaScript" type="text/javascript">function myfunction() {  income1 = document.getElementById("income1").value  income2 = document.getElementById("income2").value  expenses = document.getElementById("expenses").value  calc = income1*1 + income2*1 - expenses*1  document.getElementById("total").value = calc//  alert(20 + 50 - 15)}</script></head><body><form> Income: <input type="Text" id="income1"><br> Other income: <input type="Text" id="income2"><br> Expenses: <input type="Text" id="expenses"><br> Total: <input type="Text" value="total" id="total"><br> <input type="button" value="Calculate" onClick="myfunction()"></form></body></html> ` JMRKER

• Posts: 1
• Joined: Thu May 18, 2017 3:14 am

+ Post a reply

Page 1 of 1 