0
1
Am adding const x = document.getelementbyid.value; with const y = document.getelementbyid.value; Now when in my input box I give x = 4 and y = 6 and say z = x+y I get 46 why? Please help me am making an app.
3 Respostas
+ 3
By default, when you take input, the input is taken as string even if you input a number, so when you add those numbers, it will be concatenated instead of adding.
Like, 2 + 3 = 23 instead of 5.
So, to come out of this, you need to convert the input to integer number. To do this, use parseInt() function.
Consider the following example:
As integer:
x = parseInt(prompt("enter no. 1"))
y = parseInt(prompt("enter no. 2"))
z = x + y
alert(z)
As String:
p = prompt("enter no. 1")
q = prompt("enter no. 2")
r = p + q
alert(r)
See the the examples, hope it helps you
0
Please explain to me by using a small code
0
Thanks so much,I got you let me apply it,thank you so much