Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Array Iteration Methods Combining Array Methods Combining filter() and reduce()

I little bit confused with the starting value in this project

Hi folks, could you please help? Both the following codes work. I don't understand why the second one is good, because I can't imagine how the browser interpret this line: 'highest.price' if highest = 0 ?

The good one in teachers note:

const product = products
  .filter(product => product.price < 10)
  .reduce((highest, product) => {
    if (highest.price > product.price) {
      return highest;
    }
    return product;
  }, { price: 0 });

The bad one which also works:

 const maxcheep = products
  .filter(product => product.price < 10)
  .reduce((highest,product) => {
    if (highest.price > product.price) {  
      return highest;
    } else {

      return product;
    }

  },0); 

see here above the acc value is 0 => means that highest = 0 => how can the browser read 0.price ?

Christopher Wester
seal-mask
.a{fill-rule:evenodd;}techdegree
Christopher Wester
Full Stack JavaScript Techdegree Student 5,359 Points

Because it is read as undefined

While undefined is usually not the result we want, it does not break this particular .reduce() use.

Here, I added console.log to show the values of highest.price and product.price while running through the .reduce() portion

const products = [
  { name: 'hard drive', price: 59.99 },
  { name: 'lighbulbs', price: 2.59 },
  { name: 'paper towels', price: 6.99 },
  { name: 'flatscreen monitor', price: 159.99 },
  { name: 'cable ties', price: 19.99 },
  { name: 'ballpoint pens', price: 4.49 }
];

const maxcheep = products
.filter(product => product.price < 10)
.reduce((highest,product) => {
if (highest.price > product.price) { 
    console.log(`highest.price is currently = ${highest.price}`); 
    console.log(`product.price is currently = ${product.price}`);
    return highest;
} else {
    console.log(`highest.price is currently = ${highest.price}`); 
    console.log(`product.price is currently = ${product.price}`);
    return product;
}
},0); 

highest.price is currently = undefined
product.price is currently = 2.59
highest.price is currently = 2.59
product.price is currently = 6.99
highest.price is currently = 6.99
product.price is currently = 4.49

So the .reduce() method can function with an undefined value, because we immediately replace the undefined value when we return product.price

A better method in this use case is to NOT initialize a value at all

const products = [
  { name: 'hard drive', price: 59.99 },
  { name: 'lighbulbs', price: 2.59 },
  { name: 'paper towels', price: 6.99 },
  { name: 'flatscreen monitor', price: 159.99 },
  { name: 'cable ties', price: 19.99 },
  { name: 'ballpoint pens', price: 4.49 }
];

const maxcheep = products
.filter(product => product.price < 10)
.reduce((highest,product) => {
if (highest.price > product.price) { 
    console.log(`highest.price is currently = ${highest.price}`); 
    console.log(`product.price is currently = ${product.price}`);
    return highest;
} else {
    console.log(`highest.price is currently = ${highest.price}`); 
    console.log(`product.price is currently = ${product.price}`);
    return product;
}}); 

highest.price is currently = 2.59
product.price is currently = 6.99
highest.price is currently = 6.99
product.price is currently = 4.49

accumulator The value resulting from the previous call to callbackFn. On the first call, its value is initialValue if the latter is specified; otherwise its value is array[0].

Here is a link to MDN on Array.prototype.reduce()

Since we are using the .reduce() method to compare the prices to one another, and then discard the lower value. We do not need to set a value from outside the array.

It is most common to set an initializer to a value like 0 when we are creating a total or similar.