Skip to content

Commit 67bd688

Browse files
committed
Array Challenge - Reduce #2
1 parent f984132 commit 67bd688

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

Diff for: challenges/09-array-methods.js

+47-9
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
// have access to students from data.js
44

5-
/* map */
5+
/*
6+
=============
7+
map
8+
=============
9+
*/
610
const updatedStudents = students.map(function (student) {
711
student.role = 'student';
812
return student;
913
});
1014
// console.log(updatedStudents);
1115

12-
/* filter */
16+
/*
17+
=============
18+
filter
19+
=============
20+
*/
1321
const highScores = students.filter(function (student) {
1422
// if (student.score >= 80) {
1523
// return student;
@@ -20,13 +28,21 @@ const highScores = students.filter(function (student) {
2028
});
2129
// console.log(highScores);
2230

23-
/* find */
31+
/*
32+
=============
33+
find
34+
=============
35+
*/
2436
const specificId = students.find(function (student) {
2537
return student.id === 2;
2638
});
2739
// console.log(specificId);
2840

29-
/* reduce */
41+
/*
42+
=============
43+
reduce
44+
=============
45+
*/
3046
const averageScore =
3147
students.reduce(function (totalScore, student) {
3248
// console.log(totalScore);
@@ -35,10 +51,14 @@ const averageScore =
3551
return totalScore;
3652
}, 0) / students.length;
3753

38-
console.log(averageScore);
54+
// console.log(averageScore);
3955

40-
/* Example - Square Bracket Notation */
41-
const subject = 'physics';
56+
/*
57+
=======================
58+
Square Bracket Notation
59+
=======================
60+
*/
61+
const otherSubject = 'physics';
4262

4363
const total = {};
4464

@@ -48,6 +68,24 @@ total.history = 1;
4868
total.arts = 1;
4969

5070
// Dynamic Property Creation based on variable
51-
total[subject] = 'some value';
71+
total[otherSubject] = 'some value';
5272

53-
console.log(total);
73+
// console.log(total);
74+
75+
/*
76+
=============
77+
reduce - survey
78+
=============
79+
*/
80+
const survey = students.reduce(function (survey, student) {
81+
const favoriteSubject = student.favoriteSubject;
82+
// console.log(favoriteSubject);
83+
if (survey[favoriteSubject]) {
84+
survey[favoriteSubject] = survey[favoriteSubject] + 1;
85+
} else {
86+
survey[favoriteSubject] = 1;
87+
}
88+
return survey;
89+
}, {});
90+
91+
console.log(survey);

0 commit comments

Comments
 (0)