11import { cleanConsole , createAll } from './data' ;
22const companies = createAll ( ) ;
33
4- import f4 from './example-4' ;
5-
6- const getAvg = ( ages ) => ( ages . reduce ( ( a , b ) => a + b , 0 ) / ages . length ) ;
7-
8- export default function f5 ( us ) {
9- // 'average' => average age of "users"
10- const ages = us . map ( ( u ) => u . age ) ;
11- const ageAvg = getAvg ( ages ) ;
12-
13- // 'hasCar' => number of "users" owning a car
14- const usersWithCar = us . filter ( ( u ) => ( u . car === true ) ) ;
15-
16- return {
17- 'size' : us . length ,
18- 'average' : ageAvg ,
19- 'hasCar' : usersWithCar . length ,
20- 'averageWithCar' : getAvg ( usersWithCar . map ( ( u ) => u . age ) ) ,
21- } ;
22- }
4+ // -----------------------------------------------------------------------------
5+ // INSTRUCTIONS IN ENGLISH
236
24- cleanConsole ( 5 , companies ) ;
25- console . log ( '---- EXAMPLE 5 --- ' , f5 ( f4 ( companies ) ) ) ;
7+ // Use the function created in example 4 to create a
8+ // new function taking as parameter the "companies" variable and returning
9+ // a new object with the following attributes:
10+ // 'size' => number of "users"
11+ // 'average' => average age of "users"
12+ // 'hasCar' => number of "users" owning a car
13+ // 'averageWithCar' => average age of users with a car
2614
2715// -----------------------------------------------------------------------------
2816// INSTRUCCIONES EN ESPAÑOL
@@ -35,17 +23,6 @@ console.log('---- EXAMPLE 5 --- ', f5(f4(companies)));
3523// 'hasCar' => total de "users" propietarios de un carro
3624// 'averageWithCar' => edad promedio de los "users" con un carro
3725
38- // -----------------------------------------------------------------------------
39- // INSTRUCTIONS IN ENGLISH
40-
41- // Use the function created in example 4 to create a
42- // new function taking as parameter the "companies" variable and returning
43- // a new object with the following attributes:
44- // 'size' => number of "users"
45- // 'average' => average age of "users"
46- // 'hasCar' => number of "users" owning a car
47- // 'averageWithCar' => average age of users with a car
48-
4926// -----------------------------------------------------------------------------
5027// INSTRUCTIONS EN FRANÇAIS
5128
@@ -56,3 +33,29 @@ console.log('---- EXAMPLE 5 --- ', f5(f4(companies)));
5633// 'average' => moyenne d'âge des "users"
5734// 'hasCar' => nombre de "users" possédant une voiture
5835// 'averageWithCar' => moyenne d'âge des "users" possédant une voiture
36+
37+ import f4 from './example-4' ;
38+
39+ const getAvg = ( ages ) => ( ages . reduce ( ( a , b ) => a + b , 0 ) / ages . length ) ;
40+
41+ export default function f5 ( us ) {
42+ // Gets 'average' => average age of "users"
43+ const ages = us . map ( ( u ) => u . age ) ;
44+
45+ // Gets 'hasCar' => number of "users" owning a car
46+ const usersWithCar = us . filter (
47+ ( u ) => ( u . car === true ) ,
48+ ) ;
49+
50+ return {
51+ 'size' : us . length ,
52+ 'average' : getAvg ( ages ) ,
53+ 'hasCar' : usersWithCar . length ,
54+ 'averageWithCar' : getAvg ( usersWithCar . map (
55+ ( u ) => u . age ) ,
56+ ) ,
57+ } ;
58+ }
59+
60+ cleanConsole ( 5 , companies ) ;
61+ console . log ( '---- EXAMPLE 5 --- ' , f5 ( f4 ( companies ) ) ) ;
0 commit comments