fec-rogue / atelier-rogue

0 stars 6 forks source link

Joshua: make use of array method .reduce #42

Open eric-do opened 2 years ago

eric-do commented 2 years ago

https://github.com/fec-rogue/atelier-rogue/blob/2f1409eff5871efd01358d4d317c592904e689c8/client/src/components/reviews/Relevance.jsx#L26

Nothing wrong with this code, but try to use the array methods that are made for their purposes, to show proficiency with them.

We should be using .reduce here instead of .forEach

Instead of:

 let count = 0;
    ratingArray.forEach((value) => {
      count += Number(ratings[value]);
    })
    return count;
  }

Do:

return ratingArray.reduce((count, value) => count + Number(ratings[value]), 0)
eric-do commented 2 years ago

Also, in your Reviews component you can probably make use of .reduce there too.

Original

        const filter = Object.keys(filters);
        let filterExists = false;
        let filterArray = [];
        let results = [];
        let object = {};
        for (let value of filter) {
          if (filters[value] === true) {
            filterArray.push(value);
            filterExists = true;
          }
        }

Modified (not tested)

        const filterArray = Object.entries((filterArray, [key, value]) => {
          if (filters[value] === true) {
            filterArray.push(value)
          }
          return filterArray;
        }, [])

None of these changes are required but it's good to get proficient with array methods.