Skip to content

Commit 0d5a333

Browse files
authored
Create fp6.js
1 parent 74c0bb7 commit 0d5a333

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

fp6.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Functional Programming for Beginners Exercise
2+
3+
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];
4+
5+
// 1. Using the reduce function, create an object that
6+
// has properties for each review value, where the value
7+
// of the property is the number of reviews with that score.
8+
// for example, the answer should be shaped like this:
9+
// { 4.5: 1, 4: 2 ...}
10+
11+
const result = reviews.reduce((accumulator, currentValue) =>
12+
({
13+
...accumulator,
14+
[currentValue]: (accumulator[currentValue] || 0) + 1,
15+
}), {});
16+
17+
console.log(result)
18+
19+
//
20+
// TIP: checkout computed properties discussed here:
21+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#Computed_property_names
22+
// solution can be found at:
23+
// https://jsbin.com/dehiqux/1/edit?js,console

0 commit comments

Comments
 (0)