2

I'm new to coding, so this might be a simple question but one that I can't understand. I have an array of all the best pies each with their own price:

pieArr = [blueberry, strawberry, pumpkin, apple]

I want to create an array of objects that shows the total of the shopping cart depending on the price of the pie, and someone here on stack overflow recommended I use reduce.

This is what I have so far:

var total = 0;

const totalArr = pieArr.reduce((totalPrice, pie) => {
  if ( pie === "blueberry") {
    total += 2.5;
    totalPrice.push({["cartTotal"]:total});
    return totalPrice;
  }
 else if (pie === "apple") {
   total += 2;
   totalPrice.push({["cartTotal"]:total});
   return totalPrice;
 }, 
 [])};

What I want the end result to be is a new array of objects that keeps adding the new totals:

[{cartTotal:2.5},{cartTotal:4.5}]

The new array of objects get created, but the total doesn't get added up, so it ends up with total being 0 both times:

[{cartTotal: 0},{cartTotal: 0}]

What am I doing wrong?

6
  • 1
    const total = 0; Er, const, you sure? Also const total = total + 2.5; is invalid syntax, the code shouldn't run in the first place, I'd think? (Also, you probably want to return totalPrice when the first if condition is fulfilled as well) Commented Jun 28, 2018 at 2:30
  • In const total = total + 2.5; you’re declaring and reading your variable at the same time. Note that the outer const total = 0; is overshadowed here. Did you really mean to const-declare this? Where’s the } for the else if? Are you sure, you’re returning a value in both cases? Commented Jun 28, 2018 at 2:30
  • oh ok, so I fixed the const total, and added the return to the first if statement. however, it still doesn't change the total, any other ideas? Commented Jun 28, 2018 at 2:47
  • You should not use const if you are changing that variable. And let total be defined outside reduce operation. This way total will not be initializing after one iteration of reduce operation. Commented Jun 28, 2018 at 2:48
  • thanks CertainPerformance, Xufox, and @Meet Zaveri. I understood now what you meant by not using const if I'm changing a variable. the function works when I changed it to a var Commented Jun 28, 2018 at 2:52

2 Answers 2

4

A nice way to do this is to have a price lookup like:

let piePrices = {
    blueberry: 2.25,
    strawberry: 1.5, 
    pumpkin: 3,
    apple: 2
}

Then you can use that in map() (which is nicer than reduce() if you're just making an array from an array) without all the if/else noise:

let piePrices = {
    blueberry: 2.25,
    strawberry: 1.5, 
    pumpkin: 3,
    apple: 2
}

let pieArr = ['blueberry', 'strawberry', 'pumpkin', 'apple']

let total = 0
let totalPrice = pieArr.map(pie =>  ({cartTotal: total += piePrices[pie]}))

console.log(totalPrice)

Sign up to request clarification or add additional context in comments.

1 Comment

Mark you structured it nice. By having lookup object!
0

const total = 0;
let pieArr = ['blueberry', 'strawberry', 'pumpkin', 'apple']
const totalArr = pieArr.reduce((totalPrice, pie) => {
  let foundPie = totalPrice.find(x => x.name === pie) || {name: pie, total: 0};
  
 if ( pie === "blueberry") {
    foundPie.total += 2.5;
 }
 else if (pie === "apple") {
   foundPie.total += 2;
 }
 
 if(!totalPrice.some(x=>x.name === pie)){
   totalPrice.push(foundPie)
 }
  return totalPrice;
}, []);

console.log(totalArr)

I would recommend to add another attribute name for clearer picture. while we reducing, find the pie from accumulated totalPrice array, then do the summation, and do a checking if to push element to accumulated array

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.