new java script bug ! help

~js <script> function submiter(){

let number = document.getElementById('numba').value;
let error = 'normal error => Please enter a valid number' let text = ""; let i = 1;
if(number !== ""){
while (i < number + 1 ) { text += i + "<br>" + "<h3>"; i++;
}
document.getElementById("demo").innerHTML = text; }else{ alert(error); }
}




</script>
~
29 Replies
NIMA
NIMAā€¢3mo ago
<script>

function submiter(){


let number = document.getElementById('numba').value;

let error = 'normal error => Please enter a valid number'
let text = "";
let i = 1;

if(number !== ""){

while (i < number + 1 ) {
text += i + "<br>" + "<h3>";
i++;

}

document.getElementById("demo").innerHTML = text;
}else{
alert(error);
}

}






</script>

<script>

function submiter(){


let number = document.getElementById('numba').value;

let error = 'normal error => Please enter a valid number'
let text = "";
let i = 1;

if(number !== ""){

while (i < number + 1 ) {
text += i + "<br>" + "<h3>";
i++;

}

document.getElementById("demo").innerHTML = text;
}else{
alert(error);
}

}






</script>

when im puting like 10 in input it give me 100 not 10? anyone know why? i dont want use parsint !!! cuse then i cant use else so i put the error ! anyone know what should i do ? tnx
lko
lkoā€¢3mo ago
Because when you do while (i < number + 1 ) what you wrote as "number" is actually a string, so javascript, replacing the variables sees while (1 < "10" + 1 ) Which means while (1 < 101) { i++}
NIMA
NIMAā€¢3mo ago
what should i do if i remove + 1 it will show for example if value be 10 show 9 any thing i can do but not using pars int?
lko
lkoā€¢3mo ago
you can use parseint
NIMA
NIMAā€¢3mo ago
i know if i use thath i cant use if + else anymore
lko
lkoā€¢3mo ago
You can
NIMA
NIMAā€¢3mo ago
i cant i test and no work
while (i < number + parseInt(1) )
while (i < number + parseInt(1) )
no work
lko
lkoā€¢3mo ago
Do you know what parseInt does?
NIMA
NIMAā€¢3mo ago
yes make int
lko
lkoā€¢3mo ago
Exactly So why areyou passing it a number
NIMA
NIMAā€¢3mo ago
while (i < parsint(number) + parseInt(1) ) what 0_0 is there any way i use pars int and if + else same time? šŸ‹
Shane
Shaneā€¢3mo ago
parseint(1) and 1 are the same
NIMA
NIMAā€¢3mo ago
ye i test and i undestand thath
Tenkes
Tenkesā€¢3mo ago
your number variable is of type string. You need to parse that to number, not 1 (which is already a number). So you should do
while (i < parseInt(number) + 1)
while (i < parseInt(number) + 1)
NIMA
NIMAā€¢3mo ago
i do this but no work omg ( ) i forsget this šŸ˜” tnx so much
Tenkes
Tenkesā€¢3mo ago
no problem
Shane
Shaneā€¢3mo ago
text += i + "<br>" + "<h3>"; this is not going to do what you want it to do
NIMA
NIMAā€¢3mo ago
i know
Shane
Shaneā€¢3mo ago
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
NIMA
NIMAā€¢3mo ago
this part only br work h3 no work i fix it with style
Shane
Shaneā€¢3mo ago
its because there is no closing tag <h3> is just the open tag
NIMA
NIMAā€¢3mo ago
yes i just need the <br> h3 for test https://nim-a123.github.io/javascript1/
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢3mo ago
actually, do not do that. use parseInt outside the loop. also, you're forgetting the base-10 argument
13eck
13eckā€¢3mo ago
let number = document.getElementById('numba').valueAsNumber will get the value of your element, well, as a number. This way the browser parses as an int for you so you don't have to.
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢3mo ago
isnt that only for the number input? and how's the browser support for it?
13eck
13eckā€¢3mo ago
It's for the input element, yes, but not only for number input. And browser support is almost identical to the value property:
No description
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢3mo ago
that is very interesting
NIMA
NIMAā€¢3mo ago
nice it was helpfull