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.

5
  • 1
    should be purchases.filter(purchase => purchase.owner === "Bob") Commented Apr 9, 2021 at 14:18
  • 1
    and for the reduce part pay attention to the second arg - it's an object, not a number Commented Apr 9, 2021 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 and reduce Commented Apr 9, 2021 at 14:20
  • 1
    A common mistake 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. Commented Apr 9, 2021 at 14:41
  • Thanks for the advice! It was very helpful to see the small mistakes I was making. Commented Apr 9, 2021 at 14:51

1 Answer 1

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

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

1 Comment

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!

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.