Challenge - 5 Problems
JavaScript Interactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🔍 Analysis
intermediate2:00remaining
What is the output of this JavaScript code snippet?
Consider the following code that adds a click event listener to a button. What will be logged to the console when the button is clicked?
Intro to Computing
const button = document.createElement('button'); button.textContent = 'Click me'; document.body.appendChild(button); button.addEventListener('click', () => { console.log('Button clicked!'); }); // Simulate a click event button.click();
Attempts:
2 left
💡 Hint
Think about what happens when the click event is triggered programmatically.
✗ Incorrect
The button.click() method programmatically triggers the click event, so the event listener runs and logs 'Button clicked!'.
🧠 Conceptual
intermediate1:30remaining
Which statement best describes event bubbling in JavaScript?
When a user clicks on a nested element inside a parent element, how does event bubbling affect the event flow?
Attempts:
2 left
💡 Hint
Think about how events travel from the deepest element outward.
✗ Incorrect
Event bubbling means the event starts at the target element and then moves up through its parent elements, triggering their event handlers in order.
❓ trace
advanced2:30remaining
Trace the output of this JavaScript code with event delegation
What will be logged to the console when the user clicks on the second list item?
Intro to Computing
const list = document.createElement('ul'); ['One', 'Two', 'Three'].forEach(text => { const li = document.createElement('li'); li.textContent = text; list.appendChild(li); }); document.body.appendChild(list); list.addEventListener('click', event => { console.log(event.target.textContent); }); // Simulate clicking the second list item list.children[1].click();
Attempts:
2 left
💡 Hint
Remember that event.target is the actual clicked element, not the element with the listener.
✗ Incorrect
The event listener is on the ul, but event.target refers to the clicked li. Clicking the second li logs 'Two'.
❓ identification
advanced2:00remaining
Identify the issue in this JavaScript code for toggling a class
What will happen when the button is clicked?
Intro to Computing
const btn = document.createElement('button'); btn.id = 'myBtn'; btn.textContent = 'Click me'; document.body.appendChild(btn); btn.addEventListener('click', function() { this.classList.toggle; });
Attempts:
2 left
💡 Hint
Check how the toggle method is called.
✗ Incorrect
The code references this.classList.toggle without parentheses, so it does not call the function. No error is thrown, but no class is toggled.
❓ Comparison
expert3:00remaining
Which option correctly prevents the default form submission behavior?
Given a form with a submit button, which event handler code correctly stops the form from submitting and refreshing the page?
Intro to Computing
<form id="myForm"> <input type="text" name="name" /> <button type="submit">Submit</button> </form> <script> const form = document.getElementById('myForm'); form.addEventListener('submit', event => { // What goes here? }); </script>
Attempts:
2 left
💡 Hint
Which method is designed to stop default browser actions?
✗ Incorrect
event.preventDefault() stops the browser's default action, such as submitting a form and refreshing the page. Other options do not prevent submission correctly.