๐ Scenario: You are managing a small online store. You have a list of products with their prices. You want to find out the total price of all products combined.
๐ฏ Goal: Build a program that uses the reduce method to calculate the total price of all products in the list.
๐ What You'll Learn
Create an array of product objects with exact names and prices
Create a variable to hold the initial total price
Use the reduce method with a callback function to sum all product prices
Print the final total price
๐ก Why This Matters
๐ Real World
Calculating totals is common in shopping carts, budgets, and reports.
๐ผ Career
Knowing how to use reduce helps in data processing and summarizing information efficiently.
Progress0 / 4 steps
1
Create the products array
Create an array called products with these exact objects: { name: 'Book', price: 15 }, { name: 'Pen', price: 5 }, { name: 'Notebook', price: 10 }.
Javascript
Hint
Use square brackets [] to create an array and curly braces {} for each product object.
2
Create the initial total variable
Create a variable called initialTotal and set it to 0.
Javascript
Hint
Use const to create a variable that holds the starting total price.
3
Use reduce to calculate total price
Create a variable called totalPrice and set it to the result of calling products.reduce with a callback function that adds the current product's price to the accumulator. Use initialTotal as the starting value.
Javascript
Hint
The reduce method takes a function with two parameters: the accumulator and the current item. Add the current product's price to the accumulator.
4
Print the total price
Write a console.log statement to print the value of totalPrice.
Javascript
Hint
Use console.log(totalPrice) to show the total price in the console.
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