Challenge - 5 Problems
Timer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Node.js code output?
Consider the following code snippet using
setInterval and clearInterval. What will be printed to the console?Node.js
let count = 0; const intervalId = setInterval(() => { count++; console.log(count); if (count === 3) { clearInterval(intervalId); } }, 1000);
Attempts:
2 left
💡 Hint
Think about when
clearInterval stops the repeated action.✗ Incorrect
The interval runs every 1000ms, increasing count and printing it. When count reaches 3,
clearInterval stops further calls, so output is 1, 2, 3 then stops.📝 Syntax
intermediate1:30remaining
Which option correctly cancels a repeating timer?
You want to stop a repeating timer created by
setInterval. Which of the following code snippets correctly cancels it?Node.js
const timer = setInterval(() => console.log('tick'), 500);
Attempts:
2 left
💡 Hint
Remember the function name to stop intervals is similar to the one to start them.
✗ Incorrect
clearInterval is the correct function to stop a timer created by setInterval. clearTimeout is for setTimeout. The others are invalid functions.❓ state_output
advanced2:00remaining
What is the final value of
counter after this code runs?Analyze the code below. What will be the value of
counter after 3500 milliseconds?Node.js
let counter = 0; const id = setInterval(() => { counter += 2; if (counter >= 6) { clearInterval(id); } }, 1000);
Attempts:
2 left
💡 Hint
Count how many times the interval callback runs before stopping.
✗ Incorrect
The interval runs every 1000ms, adding 2 each time. After 3 runs (at 3000ms), counter is 6 and the interval stops. At 3500ms, counter remains 6.
🔧 Debug
advanced2:00remaining
Why does this code print odd numbers (1, 3, 5...) repeatedly?
Look at this code snippet. It prints '1', '3', '5', etc. every second instead of sequential numbers. What is the cause?
Node.js
let i = 0; setInterval(() => { i++; console.log(i++); }, 1000);
Attempts:
2 left
💡 Hint
Check how the increment operator works when used inside
console.log.✗ Incorrect
i++ is the post-increment operator, which returns the value before incrementing. The standalone i++ increments i but discards the old value. Then console.log(i++) prints the value before the second increment, resulting in 1, 3, 5, etc. The other options are incorrect.🧠 Conceptual
expert1:30remaining
What happens if you call
clearInterval with an invalid ID?In Node.js, if you call
clearInterval with a value that is not a valid interval ID, what will happen?Attempts:
2 left
💡 Hint
Think about how Node.js handles invalid timer IDs safely.
✗ Incorrect
Calling
clearInterval with an invalid or non-existent ID does nothing and does not throw an error. This design prevents crashes from accidental calls.