Challenge - 5 Problems
Array Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2:00remaining
Output of Array filter and map combination
What is the output of the following JavaScript code?
Javascript
const numbers = [1, 2, 3, 4, 5]; const result = numbers.filter(n => n % 2 === 0).map(n => n * 3); console.log(result);
Attempts:
2 left
π‘ Hint
Remember filter keeps only even numbers, then map multiplies each by 3.
β Incorrect
The filter keeps only even numbers: 2 and 4. Then map multiplies each by 3, resulting in [6, 12].
β Predict Output
intermediate2:00remaining
Result of Array reduce for sum
What is the value of
total after running this code?Javascript
const values = [10, 20, 30]; const total = values.reduce((acc, val) => acc + val, 5); console.log(total);
Attempts:
2 left
π‘ Hint
The initial value for reduce is 5, so sum starts from 5.
β Incorrect
Reduce sums all values starting from 5: 5 + 10 + 20 + 30 = 65.
β Predict Output
advanced2:00remaining
Output of Array splice method
What will be logged to the console after running this code?
Javascript
const arr = [1, 2, 3, 4, 5]; const removed = arr.splice(2, 2, 8, 9); console.log(arr); console.log(removed);
Attempts:
2 left
π‘ Hint
splice removes 2 elements starting at index 2 and inserts 8 and 9 there.
β Incorrect
splice(2, 2, 8, 9) removes elements at index 2 and 3 (3 and 4), inserts 8 and 9 at index 2. The array becomes [1, 2, 8, 9, 5]. The removed elements are [3, 4].
β Predict Output
advanced2:00remaining
Output of Array sort with compare function
What is the output of this code?
Javascript
const fruits = ['banana', 'apple', 'cherry']; fruits.sort((a, b) => b.localeCompare(a)); console.log(fruits);
Attempts:
2 left
π‘ Hint
localeCompare returns positive if a is after b alphabetically. b.localeCompare(a) sorts descending.
β Incorrect
The sort with b.localeCompare(a) sorts strings in descending alphabetical order: cherry, banana, apple.
π§ Conceptual
expert2:00remaining
Which option causes a runtime error?
Which of the following array operations will cause a runtime error when executed?
Attempts:
2 left
π‘ Hint
Look for explicit error throwing inside the callback.
β Incorrect
Option A throws an error explicitly inside forEach, causing a runtime error. Others run without errors.