why this code could not run in javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why this code could not run in javascript

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script type="text/javascript"> var number=1; var n; if(n<=10) { document.write(n+""); number(n+1); } </script> </body> </html>

3rd Dec 2019, 4:59 AM
VeVin Ganesh
VeVin Ganesh - avatar
3 Answers
+ 3
It's a complete HTML code not JavaScript code. You can run script part in JavaScript. But here in this code you declared variable number, checking n and calling function number(n+1) then how it can run.
3rd Dec 2019, 6:56 AM
A͢J
A͢J - avatar
+ 3
VeVin Ganesh There are several issues with the code. 1. Variable {n} is not initialized to a number value. 2. The variable {number} is a number, but is treated like a function which is invalid. It turns out that this variable isn't needed. 3. If you are trying to print out 1 through 10, replace {if} with {do...while} loop. 4. The value of {n} can be increased using the prefixed increment operator in the {while} break condition check. The following code resolves the issue: let n=1; do { document.write(`${n}<br/>`); } while(++n <= 10) Test this working here: https://code.sololearn.com/WhhTTiJZ9ZQc/
3rd Dec 2019, 7:41 AM
David Carroll
David Carroll - avatar
0
thanks for your informations ...
4th Dec 2019, 10:33 AM
VeVin Ganesh
VeVin Ganesh - avatar