+ 4

Help in Javascript

Why does not the program return the result of the addition and only print the numbers? https://code.sololearn.com/W4k1Y7IBqxl2/#js

30th Sep 2018, 7:04 PM
Pawel Kot
Pawel Kot - avatar
5 Respostas
+ 7
That's because the values you're using are strings, and the + concatenates them. You need to convert those strings to the type of number you want to use. If for example you want to convert them to int, you have to use parseInt. For example: function addition() { var x = document.getElementById("pole1").value; var y = document.getElementById("pole2").value; document.getElementById("result").innerHTML = parseInt(x)+parseInt(y); }
30th Sep 2018, 8:23 PM
fra
fra - avatar
+ 5
fra is parseInt() equal to Int() in python?
30th Sep 2018, 8:25 PM
Armann
Armann - avatar
+ 3
Yes Armann
30th Sep 2018, 8:29 PM
fra
fra - avatar
+ 3
thanks for help fra
30th Sep 2018, 8:42 PM
Pawel Kot
Pawel Kot - avatar
+ 2
You're very welcome :)
30th Sep 2018, 8:44 PM
fra
fra - avatar