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
1 Antwort
+ 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);