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.
4 ответов
+ 2
The + before a string (i.e. x.value ) makes javascript convert it to number... So (+x.value) is a number...
+ 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)
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
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.