What did I wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What did I wrong?

I need display numbers from 4 to 400 on the screen but it doesn't work https://code.sololearn.com/WDVW1j0j5JeO/?ref=app

11th Jul 2019, 10:02 AM
Anastasiia Kudria
Anastasiia Kudria - avatar
3 Answers
+ 5
When trying to access the dom elements, we should wait for the window to load. The code should hence be written in the onload event of window // display numbers from 5 to 500 on the screen window.onload = function(){ let out = document.getElementById("out"); let tmp = ""; for (let i=5; i<=500; i++){ tmp = tmp + i + " "; } out.innerHTML = tmp ; }
11th Jul 2019, 10:23 AM
Om Kumar
Om Kumar - avatar
+ 2
wrap the code inside a function, then add it to body onload listener. function start(){ //your code } <body onload="start()">
11th Jul 2019, 10:25 AM
Taste
Taste - avatar
+ 1
thanks for helping
11th Jul 2019, 10:41 AM
Anastasiia Kudria
Anastasiia Kudria - avatar