new java script bug ! help
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
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
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++}
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?
you can use parseint
i know
if i use thath
i cant use if + else anymore
You can
i cant
i test and no work
no work
Do you know what parseInt does?
yes
make int
Exactly
So why areyou passing it a number
while (i < parsint(number) + parseInt(1) )
what
0_0
is there any way i use pars int and if + else same time?
š
parseint(1) and 1 are the same
ye i test and i undestand thath
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
i do this but no work
omg
( )
i forsget this
š”
tnx so much
no problem
text += i + "<br>" + "<h3>";
this is not going to do what you want it to do
i know
this part only br work h3 no work
i fix it with style
its because there is no closing tag
<h3> is just the open tag
yes i just need the <br>
h3 for test
https://nim-a123.github.io/javascript1/
actually, do not do that. use
parseInt
outside the loop. also, you're forgetting the base-10 argumentlet 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.isnt that only for the number input?
and how's the browser support for it?
It's for the input element, yes, but not only for number input. And browser support is almost identical to the
value
property:that is very interesting
nice it was helpfull