JavaScript Event Handling | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

14th Mar 2022, 2:39 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
13 Answers
+ 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
14th Mar 2022, 2:45 AM
Paul K Sadler
Paul K Sadler - avatar
+ 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
15th Mar 2022, 10:08 AM
Ipang
+ 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 ..
14th Mar 2022, 2:46 AM
Ipang
+ 2
Ipang I tried for loop first and when it failed, I tried ā€œwhileā€. Thank you for this guidance.
14th Mar 2022, 2:56 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 2
Ipang It is supposed to output numbers 1 to 10 when the button is clicked.
14th Mar 2022, 3:34 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 2
Tahiti, I think you meant to log <x> rather than <num> at line 5 ... console.log( x );
14th Mar 2022, 3:37 AM
Ipang
+ 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 }
14th Mar 2022, 4:06 AM
NonStop CODING
NonStop CODING - avatar
+ 2
Ipang Thank you for this additional resource. I really appreciate your help!
15th Mar 2022, 2:46 PM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 1
Paul K Sadler Thank you. Iā€™ll check that lesson.
14th Mar 2022, 2:55 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 1
Please describe what the button was supposed to do Tahiti ...
14th Mar 2022, 3:28 AM
Ipang
+ 1
Ipang Thank you again. šŸ™
14th Mar 2022, 4:04 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
+ 1
NonStop CODING thank you for this clarification. I was actually wondering ā€œWhat about the num parameter?ā€
14th Mar 2022, 4:07 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar
0
Well, Iā€™ve retried it based on the correct syntax and the button doesnā€™t output anything Paul K Sadler Ipang .
14th Mar 2022, 3:10 AM
TahitišŸŒCastillo
TahitišŸŒCastillo - avatar