+ 2
JavaScript Event Handling
Having trouble figuring out why this code throws an error. Please help. Task: Modify the code to output the numbers 1 to 10 in a loop when the button is clicked. https://code.sololearn.com/WVP82ubjz6LK/?ref=app https://code.sololearn.com/WXE75VG8q3dA/?ref=app
13 ответов
+ 4
That is the wrong syntax for a while loop. That is the for loop syntax. Go back over the lessons for while and for loops
+ 4
Tahiti,
These pages have the extended explanation about event handler function's argument.
Basically, the first parameter of event handler accepts an event object which carries information about the event e.g. coordinates of mouse click for click event in a mouse equipped device.
* Look for a section with "Event Objects" heading ...
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events
https://eloquentjavascript.net/15_event.html
+ 2
while...loop only takes an expression (loop condition). Did you mean to use for.loop instead?
Also, there's semicolon at end of line 4. Even after changing the while loop to for...loop still won't print <num> (loop body) with that semicolon being there ..
+ 2
Ipang I tried for loop first and when it failed, I tried “while”. Thank you for this guidance.
+ 2
Ipang It is supposed to output numbers 1 to 10 when the button is clicked.
+ 2
Tahiti,
I think you meant to log <x> rather than <num> at line 5 ...
console.log( x );
+ 2
Tahiti🍷Castillo
Just change
Line 5,
change While loop to for loop and remove semicolon from its ends.
Print the value of x instead of num
// No need to use num if you are not passing anything to it just write
btn.onclick = function() {
// Your code
}
+ 2
Ipang Thank you for this additional resource. I really appreciate your help!
+ 1
Paul K Sadler Thank you. I’ll check that lesson.
+ 1
Please describe what the button was supposed to do Tahiti ...
+ 1
Ipang Thank you again. 🙏
+ 1
NonStop CODING thank you for this clarification. I was actually wondering “What about the num parameter?”
0
Well, I’ve retried it based on the correct syntax and the button doesn’t output anything Paul K Sadler Ipang .