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
NIMAOPā€¢8mo 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ā€¢8mo 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
NIMAOPā€¢8mo 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ā€¢8mo ago
you can use parseint
NIMA
NIMAOPā€¢8mo ago
i know if i use thath i cant use if + else anymore
lko
lkoā€¢8mo ago
You can
NIMA
NIMAOPā€¢8mo ago
i cant i test and no work
while (i < number + parseInt(1) )
while (i < number + parseInt(1) )
no work
lko
lkoā€¢8mo ago
Do you know what parseInt does?
NIMA
NIMAOPā€¢8mo ago
yes make int
lko
lkoā€¢8mo ago
Exactly So why areyou passing it a number
NIMA
NIMAOPā€¢8mo 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ā€¢8mo ago
parseint(1) and 1 are the same
NIMA
NIMAOPā€¢8mo ago
ye i test and i undestand thath
Tenkes
Tenkesā€¢8mo 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
NIMAOPā€¢8mo ago
i do this but no work omg ( ) i forsget this šŸ˜” tnx so much
Tenkes
Tenkesā€¢8mo ago
no problem
Shane
Shaneā€¢8mo ago
text += i + "<br>" + "<h3>"; this is not going to do what you want it to do
NIMA
NIMAOPā€¢8mo ago
i know
Shane
Shaneā€¢8mo 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
NIMAOPā€¢8mo ago
this part only br work h3 no work i fix it with style
Shane
Shaneā€¢8mo ago
its because there is no closing tag <h3> is just the open tag
NIMA
NIMAOPā€¢8mo ago
yes i just need the <br> h3 for test https://nim-a123.github.io/javascript1/
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢8mo ago
actually, do not do that. use parseInt outside the loop. also, you're forgetting the base-10 argument
13eck
13eckā€¢8mo 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.
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢8mo ago
isnt that only for the number input? and how's the browser support for it?
13eck
13eckā€¢8mo 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
į¼”ĻĻ‰Ļ‚
į¼”ĻĻ‰Ļ‚ā€¢8mo ago
that is very interesting
NIMA
NIMAOPā€¢8mo ago
nice it was helpfull
Want results from more Discord servers?
Add your server