how to make JavaScript calculate this correctly?

how to make JavaScript calculate this correctly?

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 - expensescalc = (income1 + income2) - expensescalc = 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.

Re: how to make JavaScript calculate this correctly?

The '+' character does addition of numbers AND the concatenation of strings.The format of your input element values are strings until converted to numbersOne 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.