Javascript Addition Not Working
-
This is probably has a simple answer but I have been working on this for hours without being able to figure it out. I have the following function that retrieves a value ‘spot’ from input field ‘myInput’ and calculates a value ‘tmpspot’.
For this example I am entering the number ’23’ in the field “myInput” (type=”number”). When I pass ’23’ to the function as shown below the function properly returns ’44’. However, when I comment out the line “tmpspot = tmpspot – (0 – spot) ;” and instead use the previous line “tmpspot = tmpspot + spot ;”, the function returns ‘2123’, as though it concatenated the two numbers instead of adding them together.
What am I missing?
function NextSpot() {
var spot = document.getElementById(“myInput”).value;
var tmpspot = spot % 2 ;
tmpspot = (tmpspot – (0 – spot)) – 1 ;
tmpspot = 65 – spot ;
tmpspot = tmpspot / 2 ;
tmpspot = parseInt(tmpspot);
// tmpspot = tmpspot + spot ;
tmpspot = tmpspot – (0 – spot) ;
document.getElementById(“demo”).innerHTML = tmpspot ;
return true ;
}
- The topic ‘Javascript Addition Not Working’ is closed to new replies.