The reduce method processes an array and combines all its elements into a single value by applying a function repeatedly.
Click to reveal answer
beginner
What are the two main arguments of the reduce method callback function?
The callback function takes two main arguments: accumulator (the combined value so far) and currentValue (the current element being processed).
Click to reveal answer
intermediate
What is the purpose of the initial value in the reduce method?
The initial value sets the starting point for the accumulator. If not provided, the first array element is used as the initial accumulator value.
Click to reveal answer
beginner
How would you use reduce to sum all numbers in an array?
You pass a callback that adds the accumulator and current value, and set the initial value to 0. Example: [1,2,3].reduce((acc, val) => acc + val, 0) returns 6.
Click to reveal answer
intermediate
Can reduce be used to transform an array into an object? How?
Yes! By using the accumulator as an object and adding properties inside the callback, you can build an object from array elements.
Click to reveal answer
What does the accumulator represent in the reduce method?
AThe combined result so far
BThe current element in the array
CThe index of the current element
DThe original array
✗ Incorrect
The accumulator holds the combined value returned by the callback after processing each element.
What happens if you do not provide an initial value to reduce?
AThe first element of the array is used as the initial accumulator
BThe method throws an error
CThe accumulator starts as undefined
DThe method returns the original array
✗ Incorrect
If no initial value is given, reduce uses the first array element as the accumulator and starts from the second element.
Which of these is a valid use of reduce?
ACreating a new array with map
BFiltering elements from an array
CSorting an array
DSumming numbers in an array
✗ Incorrect
Reduce is commonly used to combine array elements, such as summing numbers.
How many arguments does the callback function of reduce receive?
A2
B3
C4
D1
✗ Incorrect
The callback receives 4 arguments: accumulator, currentValue, currentIndex, and the original array.
Which of these is NOT a typical use case for reduce?
AFlattening an array of arrays
BReplacing elements in an array
CCalculating a product of numbers
DCounting occurrences of items
✗ Incorrect
Replacing elements is better done with map or forEach, not reduce.
Explain how the reduce method works in JavaScript and give a simple example.
Think about how you combine items one by one into a single result.
You got /5 concepts.
Describe a real-life situation where using reduce would be helpful.
Imagine you want to add up prices or count items in a list.
You got /4 concepts.
Practice
(1/5)
1. What does the reduce method do in JavaScript?
easy
A. It combines all array elements into a single value.
B. It creates a new array with filtered elements.
C. It sorts the elements of an array.
D. It adds a new element to the end of an array.
Solution
Step 1: Understand the purpose of reduce
The reduce method processes each element of an array to produce one combined result.
Step 2: Compare with other array methods
Unlike filter or sort, reduce returns a single value, not a new array or sorted array.
Final Answer:
It combines all array elements into a single value. -> Option A
Quick Check:
Reduce = combine into one value [OK]
Hint: Reduce combines array items into one value quickly [OK]
Common Mistakes:
Confusing reduce with filter or map
Thinking reduce returns an array
Assuming reduce sorts elements
2. Which of the following is the correct syntax to use reduce on an array arr to sum its elements (safe for empty arrays)?
easy
A. arr.reduce((acc, val) => acc + val, 0);
B. arr.reduce((acc, val) => acc + val);
C. arr.reduce(acc, val => acc + val);
D. arr.reduce((val, acc) => val + acc);
Solution
Step 1: Check the callback function parameters
The callback must have (accumulator, currentValue) in that order.
Step 2: Verify initial value usage
Providing 0 as the initial value ensures correct sum starting point.
Final Answer:
arr.reduce((acc, val) => acc + val, 0); -> Option A
Quick Check:
Correct syntax with initial value = arr.reduce((acc, val) => acc + val, 0); [OK]
Hint: Use (acc, val) and initial value for sum [OK]
Common Mistakes:
Swapping accumulator and current value parameters
Omitting initial value causing errors on empty arrays