Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit cecd9e6

Browse files
committed
feat: Coding Challenge 3 of Arrays Section Done
1 parent ec4d96b commit cecd9e6

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

challenges/section11-challenges.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,42 @@ const dogsAge2 = [16, 6, 10, 5, 6, 1, 4];
7777

7878
const calcAverageHumanAge = ages =>
7979
{
80-
const humanYears = ages.map(curr => (curr <= 2) ? curr * 2 : 16 + curr * 4);
81-
const humanYearsFiltered = humanYears.filter(curr => curr >= 18);
80+
const humanYears = ages.map(age => (age <= 2) ? age * 2 : 16 + age * 4);
81+
const humanYearsFiltered = humanYears.filter(age => age >= 18);
8282
console.log(humanYearsFiltered);
8383

84-
let avr = humanYearsFiltered.reduce((acc, curr) => acc += curr, 0);
84+
let avr = humanYearsFiltered.reduce((acc, age) => acc += age, 0);
8585
avr /= humanYearsFiltered.length;
86-
console.log(`Average of Human Years: ${ avr }`);
86+
console.log(`Human Years Average: ${ avr }`);
8787
};
8888

8989
console.log('----- TEST 1 -----');
9090
calcAverageHumanAge(dogsAge1);
9191

9292
console.log('----- TEST 2 -----');
9393
calcAverageHumanAge(dogsAge2);
94+
95+
// Coding Challenge 2
96+
97+
/*
98+
Rewrite the 'calcAverageHumanAge' function from Challenge #2, but this time
99+
as an arrow function, and using chaining!
100+
Test data:
101+
§ Data 1: [5, 2, 4, 1, 15, 8, 3]
102+
§ Data 2: [16, 6, 10, 5, 6, 1, 4]
103+
GOOD LUCK 😀
104+
*/
105+
106+
const calcAverageHumanAgeRev = ages =>
107+
{
108+
let avr = ages.map(age => (age <= 2) ? age * 2 : 16 + age * 4)
109+
.filter(age => age >= 18)
110+
.reduce((acc, age, i, arr) => acc + age / arr.length, 0);
111+
console.log(`Human Years Average: ${ avr }`);
112+
};
113+
114+
console.log('----- TEST 1 REV -----');
115+
calcAverageHumanAgeRev(dogsAge1);
116+
117+
console.log('----- TEST 2 REV -----');
118+
calcAverageHumanAgeRev(dogsAge2);

0 commit comments

Comments
 (0)