# how to make JavaScript calculate this correctly?

10 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.
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

}
</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

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

by patsm00re18 » Thu Sep 05, 2019 1:24 am

thanks for the very useful facts! world of solitaire

patsm00re18

• Posts: 35
• Joined: Wed Aug 07, 2019 12:54 pm

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

by mazikeen » Tue Sep 10, 2019 2:44 pm

mazikeen

• Posts: 26
• Joined: Tue Sep 10, 2019 2:37 pm

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

by Rheygo » Wed Jul 29, 2020 4:23 pm

Thanks a lot for sharing this JS entry! I need some how the same as this for my residential and commercial electrical services computation.

Rheygo

• Posts: 8
• Joined: Mon May 13, 2019 12:16 pm

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

by Michele616 » Tue Aug 18, 2020 4:36 pm

Nice post! Try using JSFiddle code editor for that equation. Hope it works! Thanks! Fairfax siding repair services

Michele616

• Posts: 4
• Joined: Tue Aug 18, 2020 1:34 pm

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

by Chris889 » Wed Aug 19, 2020 5:58 pm

We have the same problem I think. Thanks for providing the solution.
Travel in the Time of Coronavirus: Is it Safe? Best Acoustic Guitar Strings

Chris889

• Posts: 11
• Joined: Tue Nov 06, 2018 5:45 pm

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

by RheedGo » Thu Sep 17, 2020 3:43 pm

Nice post! Just like my gas & refueling needs to calculate the remaining fuel o avoid being empty.

RheedGo

• Posts: 12
• Joined: Sun Jul 26, 2020 1:29 pm

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

by MR136 » Tue Sep 22, 2020 2:23 pm

Nice JS post! How bout trying some JS code editor online.
MR - roofing contractor Manassas VA

MR136

• Posts: 2
• Joined: Tue Jul 21, 2020 6:12 pm

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

by karlyle4419 » Tue Sep 22, 2020 3:16 pm

Hope this works! I need a this script for my best bookkeepIng services in Arlington

karlyle4419

• Posts: 1
• Joined: Tue Sep 22, 2020 3:09 pm