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.

22nd May 2020, 6:08 AM
louis wambua
louis wambua - avatar
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
22nd May 2020, 6:19 AM
Bibek Oli
Bibek Oli - avatar
0
Please explain to me by using a small code
22nd May 2020, 6:16 AM
louis wambua
louis wambua - avatar
0
Thanks so much,I got you let me apply it,thank you so much
22nd May 2020, 6:23 AM
louis wambua
louis wambua - avatar