0

How can I add integer to integer after I input values in it.

Better see my code "grading system", after I pushed submit button. The values aren't adding but instead they concat.

29th Jul 2019, 6:44 PM
Johnny Deguzman
Johnny Deguzman - avatar
4 Answers
+ 2
The + before a string (i.e. x.value ) makes javascript convert it to number... So (+x.value) is a number...
30th Jul 2019, 3:22 AM
unChabon
unChabon - avatar
+ 2
var a = '4'; var b = '8'; console.log(a + b); // 48 console.log((+a) + (+b)); // 12 (Post your code here if this doesn't help)
29th Jul 2019, 7:28 PM
unChabon
unChabon - avatar
0
diego Code thanks mr. The logic you've given– it worked. I copied your operators. Can you explain it to me. Why it needs parentheses and have their own (Math Signs) before executing. Thanks
30th Jul 2019, 3:11 AM
Johnny Deguzman
Johnny Deguzman - avatar
0
diego Code here's my code now. var x, y, z, c, all; x = document.getElementById("num1"); y = document.getElementById("num2"); z = document.getElementById("num3"); c = document.getElementById("num4"); all = ((+x.value) + (+y.value) + (+z.value) + (+c.value)) ; document.getElementById("here").innerHTML = all; It doesn't concat anymore because of your parentheses and every values' operator.
30th Jul 2019, 3:16 AM
Johnny Deguzman
Johnny Deguzman - avatar