My times table js works, but not when I get initial value from input box. Why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

My times table js works, but not when I get initial value from input box. Why?

This js writes the times table for "var s", and it works: var s = 3 var i = s; var d =10*s for (; i <= d; ) { document.write(i+ "<br>"); i+=s;} This other js is supposed to do the same thing as above, but using the value inserted in an input box. But it only writes the inserted value. Could anyone please help me understand why? function getInputValue(){ var s = document.getElementById("myInput").value; var i = s; var d =10*s for (; i <= d; ) { document.write(i+ "<br>"); i+=s;}} Thanks! https://code.sololearn.com/WNN550TTE0SE

27th Apr 2020, 1:09 PM
Loris Matha
Loris Matha - avatar
1 Answer
+ 1
That's because the input value is a string. Use parseInt() to convert it to a number. var s = parseInt(document.getElementById("myInput").value);
27th Apr 2020, 2:04 PM
yochanan sheinberger
yochanan sheinberger - avatar