2
I’m having trouble making an account using javascript:
javascript
num1 = $("#num1").val(); // exemplo 2
num2 = $("#num2").val(); // exemplo 50
result = num1+num2;
This code results in 250 instead of 52
It looks like he’s concatenating instead of adding.
How to fix this mistake?
It returns 250 because it is assuming that you have two strings.
– CesarMiguel
Whenever using parseint use (value, Radix), because in javascript numbers starting with 0 are of the octal type, so if the value of "#num1" and "#num2" is for example 03 and 05 it will do the sum in octal, to avoid vc should use
parseInt($("#num1").val(), 10)
:)– Leandro Amorim
This is not an error the val() method of jQuery returns a String the javascript operator "+" serves both to add and to concatenate, I suggest you take a look at http://www.codecademy.com/
– Tuyoshi Vinicius