Challenge - 5 Problems
Array Iteration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediateOutput of forEach with asynchronous callback
What is the output of this code snippet?
Javascript
const arr = [1, 2, 3]; arr.forEach(async (num) => { await new Promise(resolve => setTimeout(resolve, 10)); console.log(num * 2); }); console.log('Done');
Attempts:
2 left
π‘ Hint
Remember that forEach does not wait for async callbacks to finish.
β Incorrect
The forEach loop starts all async callbacks but does not wait for them. So 'Done' is logged immediately, then after delays, 2, 4, 6 are logged.
β Predict Output
intermediateOutput of map with side effects
What is the output of this code?
Javascript
const numbers = [1, 2, 3]; const result = numbers.map((num, index) => { console.log(index); return num * 2; }); console.log(result);
Attempts:
2 left
π‘ Hint
map calls the callback for each element and returns a new array.
β Incorrect
The map callback logs the index for each element (0,1,2) then returns doubled values. The final console.log prints the new array.
π§ Debug
advancedWhy does this for...of loop skip the last element?
Consider this code. Why does it skip printing the last element?
const arr = [10, 20, 30];
for (let i of arr) {
if (i === 20) break;
console.log(i);
}
Javascript
const arr = [10, 20, 30]; for (let i of arr) { if (i === 20) break; console.log(i); }
Attempts:
2 left
π‘ Hint
What does the break statement do inside a loop?
β Incorrect
The break stops the loop immediately when i equals 20, so the last element 30 is never processed or printed.
π Syntax
advancedIdentify the syntax error in this array iteration
Which option contains the syntax error?
Attempts:
2 left
π‘ Hint
Check the order of keywords in the for loop syntax.
β Incorrect
Option B has incorrect syntax: 'for arr in i' is invalid. The correct syntax is 'for (let i of arr)'.
π Application
expertFind the sum of squares of even numbers using array iteration
Which code snippet correctly calculates the sum of squares of even numbers in the array [1,2,3,4,5,6]?
Attempts:
2 left
π‘ Hint
Filter first to keep even numbers, then square, then sum.
β Incorrect
Option A filters even numbers first, then squares them, then sums. Option A squares all first, then filters even squares (wrong). Option A sums squares of even numbers correctly but uses reduce only. Option A sums squares of all numbers (wrong).
