Proceed to Checkout explainer


#1

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()


#2