Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

JavaScript Reducer

const reviews = [4.5, 4.0, 5.0, 2.0, 1.0, 5.0, 3.0, 4.0, 1.0, 5.0, 4.5, 3.0, 2.5, 2.0];
const reviewsLetter = ['c', 'd', 'f', 'a', 'g', 'a', 'd', 'f', 'e', 'b'];
// 1. Using the reduce function, create an object that// has properties for each review value, where the value// of the property is the number of reviews with that score.// for example, the answer should be shaped like this:// { 4.5: 1, 4: 2 ...}
const countGroupedByReview = reviewsLetter.reduce(groupBy, {});
function groupBy (acc, review){  
  const count = acc[review] || 0;  
  return {...acc, [review]: count + 1}}
console.log(countGroupedByReview);
Source by jsbin.com #
 
PREVIOUS NEXT
Tagged: #JavaScript #Reducer
ADD COMMENT
Topic
Name
5+7 =