Lab
JavaScript Engine Simulator
20 min
Intermediate3 Free Attempts
Instructions
Objective
Implement three functions that test your mastery of JavaScript async patterns — the exact skills frontend interviews assess.
Task 1: Predict Event Loop Output
Write a function predictOutput() that returns an array of strings in the exact order they would be logged:
function predictOutput() {
// Return the order of console.logs for this code:
//
// console.log('A');
// setTimeout(() => console.log('B'), 0);
// Promise.resolve()
// .then(() => console.log('C'))
// .then(() => console.log('D'));
// queueMicrotask(() => console.log('E'));
// console.log('F');
return ['A', '?', '?', '?', '?', '?']; // Replace with correct order
}
Task 2: Implement Promise.all
Write a promiseAll(promises) function that behaves identically to Promise.all:
- Resolves with an array of results when all promises fulfill
- Results must maintain the same order as the input promises
- Rejects immediately when any promise rejects
- Handles non-Promise values (wraps them with Promise.resolve)
- Handles empty array input (resolves with [])
function promiseAll(promises) {
// Your implementation here
}
Task 3: Retry with Exponential Backoff
Write a retryWithBackoff(fn, maxRetries, baseDelay) function:
- Calls
fn()and returns its result if successful - On failure, backs off exponentially: attempt 0 waits ~
baseDelay, attempt 1 ~baseDelay * 2, attempt 2 ~baseDelay * 4 maxRetriescounts retries after the first attempt, somaxRetries = 3makes up to 4 calls tofn. When they are all exhausted, rethrow the last error- Jitter is accepted and encouraged — randomising each wait within its backoff window (for example 50-100% of it) is the version you should defend in an interview, and the lesson explains why. A pure
baseDelay * 2^attemptwait is also accepted - Must work with both sync and async functions
async function retryWithBackoff(fn, maxRetries = 3, baseDelay = 1000) {
// Your implementation here
}
Requirements
- All three functions must be implemented
- Code must be valid JavaScript (ES2022+)
- No external libraries allowed
- Include brief comments explaining your reasoning for Task 1
What to Submit
Your submission should contain 1 file section in the editor below: a JavaScript file with all 3 functions.
Grading Rubric
Task 1: Correctly predicts the event loop output order (A, F, C, E, D, B) with accurate reasoning about microtask vs macrotask queues25 points
Task 2: Correctly implements Promise.all with proper order preservation, immediate rejection, non-Promise handling, and empty array edge case35 points
Task 3: Implements retry with exponential backoff anchored on baseDelay and doubling per attempt (jitter within each backoff window is correct and must not be penalised), makes maxRetries + 1 calls in the worst case before rethrowing the last error, and works with both sync and async functions30 points
Code quality: Clean, readable code with appropriate comments explaining the event loop reasoning10 points
Checklist
0/7Your Solution
3 free attempts remaining