3

I have an array:

const products = [
    { product: 'banana', price: 3 },
    { product: 'mango', price: 6 },
    { product: 'potato', price: ' ' },
    { product: 'avocado', price: 8 },
    { product: 'coffee', price: 10 },
    { product: 'tea', price: '' },
  ]
  

And Iwould like to sum all prices. What I tried:

const sum = products.reduce(function(acc, cur){
if (Number.isInteger(cur.price))
  return acc+cur.price

  }, 0)
 console.log(sum)

it returns undefined. I also tried that without the condition, it returns a string. Where do I make a mistake?

3
  • 1
    You forgot to declare products in the second snippet. Also, the first snippet is unnecessary; just use a code block if your code doesn't have any output. Commented Oct 18, 2020 at 16:25
  • You need a return for else also Commented Oct 18, 2020 at 16:26
  • Return the accumulator after the if-condition Commented Oct 18, 2020 at 16:29

3 Answers 3

5

You need to return the accumulator if the condition is not true.

const
    products = [{ product: 'banana', price: 3 }, { product: 'mango', price: 6 }, { product: 'potato', price: ' ' }, { product: 'avocado', price: 8 }, { product: 'coffee', price: 10  }, { product: 'tea', price: '' }],
    sum = products.reduce(function(acc, cur) {
        if (Number.isInteger(cur.price)) return acc + cur.price;
        else return acc;
    }, 0);
    
console.log(sum);

A shorter approach adds the value conditionally and returns only the accumulator at the end.

const
    products = [{ product: 'banana', price: 3 }, { product: 'mango', price: 6 }, { product: 'potato', price: ' ' }, { product: 'avocado', price: 8 }, { product: 'coffee', price: 10  }, { product: 'tea', price: '' }],
    sum = products.reduce(function(acc, cur) {
        if (Number.isInteger(cur.price)) acc += cur.price;
        return acc;
    }, 0);
    
console.log(sum);

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

Comments

1

Try this:

const products = [
    { product: 'banana', price: 3 },
    { product: 'mango', price: 6 },
    { product: 'potato', price: ' ' },
    { product: 'avocado', price: 8 },
    { product: 'coffee', price: 10 },
    { product: 'tea', price: '' },
  ]

const sum = products.filter(x => typeof x.price === 'number').map(x => x.price).reduce((a, b) => a + b);
console.log(sum)

Comments

1

The result of your accumulator for the last element tea is undefined because you don't return anything, if price is not a number. And as this is the last call to the accumulator, also the result of reduce is undefined.

If you just want to ignore values with invalid price, you can either use

products.filter(x => Number.isInteger(x.price)).reduce (...)

to only sum up products with a valid price

or return for instance acc if cur.price is not a number.

const sum = products.reduce(function(acc, cur){
  if (Number.isInteger(cur.price)) 
    return acc+cur.price;
  return acc;
  }, 0)
 console.log(sum)

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.