1

I am trying to use filter and reduce to get the total price of Bob's purchases for a little practice on higher order functions I'm doing. I'm given

const purchases = [{"owner":"Barry","price":103},{"owner":"Bob","price":75},
{"owner":"Bob","price":73},{"owner":"Barry","price":57},{"owner":"Barry","price":128},
{"owner":"Bob","price":119},{"owner":"Barry","price":133},{"owner":"Barry","price":27},
{"owner":"Barry","price":138},{"owner":"Bob","price":68},{"owner":"Bob","price":50},
{"owner":"Barry","price":9},{"owner":"Bob","price":123},{"owner":"Bob","price":135},
{"owner":"Barry","price":30},{"owner":"Barry","price":129},{"owner":"Barry","price":38},
{"owner":"Bob","price":133},{"owner":"Barry","price":109},{"owner":"Bob","price":115}]

Use a high order method to create to get the sum of bobsTotal.

This is what I am thinking. I am able to filter bob's purchases into an array, but I am having trouble getting the total price now. I have my reduce section commented out for now for log purposes.

let bobsTotal = purchases.filter(total=>total=purchases.owner="Bob")//.reduce((total, price) => total + price)
console.log(bobsTotal)

Any advice would be great or any other ways to do this would be great.

  • 1
    should be `purchases.filter(purchase => purchase.owner === "Bob")` – georg Apr 09 '21 at 14:18
  • 1
    and for the `reduce` part pay attention to the second arg - it's an object, not a number – georg Apr 09 '21 at 14:19
  • 1
    You are on the right track. Your callback functions for `filter` and `reduce` are incorrect. Take a look at the examples in the documentation here: [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) and [reduce](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) – Will Apr 09 '21 at 14:20
  • 1
    A [common mistake](https://stackoverflow.com/q/5732043/4642212) would be `.reduce((total, price) => total.price + price.price)`. But why do you think that the `price` property gets somehow automatically accessed in `.reduce((total, price) => total + price)`? `.reduce((total, {price}) => total + price, 0)` would work. – Sebastian Simon Apr 09 '21 at 14:41
  • Thanks for the advice! It was very helpful to see the small mistakes I was making. – Justin Valdez Apr 09 '21 at 14:51

1 Answers1

1

You're very close.

filter's callback needs to be a function that returns true or false.

reduce's callback's arguments are aggregate (because you are aggregating a list into one thing) and current value, and you'll want to set a default value of 0, to have something to sum from.

const purchases = [{"owner":"Barry","price":103},{"owner":"Bob","price":75},
{"owner":"Bob","price":73},{"owner":"Barry","price":57},{"owner":"Barry","price":128},
{"owner":"Bob","price":119},{"owner":"Barry","price":133},{"owner":"Barry","price":27},
{"owner":"Barry","price":138},{"owner":"Bob","price":68},{"owner":"Bob","price":50},
{"owner":"Barry","price":9},{"owner":"Bob","price":123},{"owner":"Bob","price":135},
{"owner":"Barry","price":30},{"owner":"Barry","price":129},{"owner":"Barry","price":38},
{"owner":"Bob","price":133},{"owner":"Barry","price":109},{"owner":"Bob","price":115}]

let bobtot = purchases.filter(p => p.owner === 'Bob').reduce((sum, p) => sum+p.price, 0);

console.log('bob\'s total:', bobtot);
Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
  • Thanks Matt. This worked great and makes sense. I'm glad I was at least on the right track, just needed a little nudge from all you great overflowers! – Justin Valdez Apr 09 '21 at 14:53