Proceed to Checkout explainer

The aim of this puzzle: Complete the arrow functions to get the prices array and total value.

Walkthrough of the solution: When you run the starter code, there are some strange things happening; there is undefined and NaN being output. We can see that the arrow functions in .map() and .reduce() are both empty, so we know they are not returning anything which is a problem.

The prices variable should be an array of just the .price value of each item in the cart. The arrow function should have an input parameter called item and it should return the item.price. Between the curly brackets {} of the arrow function, add return item.price. Now, the prices array can be created correctly.

Next, the total can be calculated using .reduce(). The .reduce() methode also uses a callback function. That function should take in 2 arguments: an accumulator and the current item. In this puzzle, they’re named sum and price, since it is going through each item in the prices and adding them to the sum. The arrow function should update the sum using the price value and then return that value. Since the total should store all the prices added up, the arrow function should return sum + price.

Sample code solution:
(Tap below to reveal)

Since this is part of the final topic of the Arrays course and it is testing your knowledge, there is no sample solution code.

JavaScript Concepts: Arithmetic Operators (+), Binary Expression (+ concatenation), Code Block (arrow function), Conditionals (ternary statement), console.log, import, .map(), .reduce()
Additional Code (hidden code that runs before the puzzle’s code):

let cart = [
    {name: 'towel', price: 15},
    {name: 'soap', price: 6},
    {name: 'comb', price: 4},
    {name: 'sponge', price: 2}

it should return the item.price, return is not accepted

Hey there, can you post a screenshot of your code?

1 Like

A post was merged into an existing topic: Hoppy Grace Explainer

Mine is showing I’m incorrect but I have the right answer.

Could you post a screenshot of your code?


I think the only change needed is the ternary (back to how the starter code had it).

let shipping = total > 25 ? 0 + 0 : __
let shipping = total > 25 ? 0 : 10


Hey there,

We want the prices variables to store a list of all the prices from the cart, so it just needs to have return item.price in its .map() function.

Right now you have a variable called sum inside the .map() function, but sum is scoped to the .reduce() function in the total variable, so the .map() function can’t see it.

Hope that helps!

1 Like