Skip to content

Commit e318e60

Browse files
committed
completed wesbos#8, reduce exercise, sum up the instances of each element of array
1 parent f5594da commit e318e60

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

04 - Array Cardio Day 1/index-START.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@
8282
// 8. Reduce Exercise
8383
// Sum up the instances of each of these
8484
const data = ['car', 'car', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'bike', 'walk', 'car', 'van', 'car', 'truck' ];
85+
// let countsOfVehicleTypes = {};
86+
let countsOfVehicleTypes = data.reduce((acc, curr) => {
87+
// if (acc[curr]) { //not bulletproof as tests for absence of all falsey values
88+
if (curr in acc) { // better coding
89+
acc[curr] += 1;
90+
} else {
91+
acc[curr] = 1;
92+
}
93+
return acc;
94+
}, {});
95+
console.table(countsOfVehicleTypes);
96+
8597

8698
</script>
8799
</body>

0 commit comments

Comments
 (0)