• Resolved carl2e

    (@carl2e)


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

Viewing 1 replies (of 1 total)
  • Thread Starter carl2e

    (@carl2e)

    I realized that php was treating the variables as string variables, not numeric variables. For now, I including the following line to make sure that the variable ‘spot’ is an integer:

    spot = parseInt(spot) ;

    The function is now working.

Viewing 1 replies (of 1 total)
  • The topic ‘Javascript Addition Not Working’ is closed to new replies.