0
0
Intro to Computingfundamentals~20 mins

JavaScript for interactivity in Intro to Computing - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JavaScript Interactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🔍 Analysis
intermediate
2: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();
Aundefined
BButton clicked!
CSyntaxError
DNo output
Attempts:
2 left
💡 Hint
Think about what happens when the click event is triggered programmatically.
🧠 Conceptual
intermediate
1: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?
AThe event triggers handlers on the clicked element and then moves up to its ancestors.
BThe event triggers handlers randomly on any element in the document.
CThe event triggers handlers on the ancestors first, then on the clicked element.
DThe event triggers handlers only on the clicked element, ignoring ancestors.
Attempts:
2 left
💡 Hint
Think about how events travel from the deepest element outward.
trace
advanced
2: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();
ATwo
BOne
Cundefined
DSyntaxError
Attempts:
2 left
💡 Hint
Remember that event.target is the actual clicked element, not the element with the listener.
identification
advanced
2: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;
});
ATypeError: this.classList.toggle is not a function
BSyntaxError: missing parentheses
CNo error thrown
DReferenceError: btn is not defined
Attempts:
2 left
💡 Hint
Check how the toggle method is called.
Comparison
expert
3: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>
Aevent.stopPropagation(); console.log('Form submission stopped');
Bevent.defaultPrevented = true; console.log('Form submission stopped');
Creturn false; console.log('Form submission stopped');
Devent.preventDefault(); console.log('Form submission stopped');
Attempts:
2 left
💡 Hint
Which method is designed to stop default browser actions?