0
0
Node.jsframework~10 mins

Event loop phases and timer execution in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to schedule a function to run after 1000 milliseconds.

Node.js
setTimeout(() => { console.log('Hello after 1 second'); }, [1]);
Drag options to blanks, or click blank then click option'
Anull
B10
C0
D1000
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or null will cause the callback to run immediately or not at all.
Confusing seconds with milliseconds.
2fill in blank
medium

Complete the code to schedule a function to run repeatedly every 500 milliseconds.

Node.js
const intervalId = setInterval(() => { console.log('Repeating every 0.5 seconds'); }, [1]);
Drag options to blanks, or click blank then click option'
A500
B1000
C50
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 will cause the callback to run as fast as possible, which is not intended.
Confusing interval time with timeout delay.
3fill in blank
hard

Fix the error in the code to clear the interval after 3 seconds.

Node.js
const id = setInterval(() => { console.log('Tick'); }, 1000);
setTimeout(() => { clearInterval([1]); }, 3000);
Drag options to blanks, or click blank then click option'
AtimerId
BintervalId
Cid
DtimeoutId
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that was never declared.
Confusing interval ID with timeout ID.
4fill in blank
hard

Fill both blanks to create a Promise that resolves after 2 seconds using setTimeout.

Node.js
const delay = ms => new Promise(resolve => {
  setTimeout(() => {
    [1]();
  }, [2]);
});
Drag options to blanks, or click blank then click option'
Aresolve
Bms
Cconsole.log
DclearTimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Calling reject instead of resolve.
Using console.log instead of resolve.
Passing wrong delay time.
5fill in blank
hard

Fill all three blanks to create a map of words to their lengths, but only for words longer than 3 characters.

Node.js
const words = ['apple', 'bat', 'carrot', 'dog'];
const lengths = words.reduce((acc, [3]) => {
  if ([3].length > 3) {
    acc[[1]] = [2];
  }
  return acc;
}, {});
Drag options to blanks, or click blank then click option'
Aword
Bword.length
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Using length as key instead of word.
Not filtering words by length.