-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-5.js
More file actions
executable file
·61 lines (49 loc) · 2.14 KB
/
example-5.js
File metadata and controls
executable file
·61 lines (49 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import {cleanConsole, createAll} from './data';
const companies = createAll();
// -----------------------------------------------------------------------------
// INSTRUCTIONS IN ENGLISH
// Use the function created in example 4 to create a
// new function taking as parameter the "companies" variable and returning
// a new object with the following attributes:
// 'size' => number of "users"
// 'average' => average age of "users"
// 'hasCar' => number of "users" owning a car
// 'averageWithCar' => average age of users with a car
// -----------------------------------------------------------------------------
// INSTRUCCIONES EN ESPAÑOL
// Use la función creada en el ejemplo 4 para crear una nueva función tomando
// como parámetro la variable "companies" y devuelve un nuevo objeto con los
// siguientes atributos:
// 'size' => total de "users"
// 'average' => edad promedio de "users"
// 'hasCar' => total de "users" propietarios de un carro
// 'averageWithCar' => edad promedio de los "users" con un carro
// -----------------------------------------------------------------------------
// INSTRUCTIONS EN FRANÇAIS
// Utiliser la fonction créée dans l'exemple 4 pour créer une
// nouvelle fonction prenant en paramètre la variable "companies" et renvoyant
// un nouvel objet avec les attributs suivants :
// 'size' => nombre de "users"
// 'average' => moyenne d'âge des "users"
// 'hasCar' => nombre de "users" possédant une voiture
// 'averageWithCar' => moyenne d'âge des "users" possédant une voiture
import f4 from './example-4';
const getAvg = (ages) => (ages.reduce((a, b) => a + b, 0) / ages.length);
export default function f5(us) {
// Gets 'average' => average age of "users"
const ages = us.map((u) => u.age);
// Gets 'hasCar' => number of "users" owning a car
const usersWithCar = us.filter(
(u) => (u.car === true),
);
return {
'size': us.length,
'average': getAvg(ages),
'hasCar': usersWithCar.length,
'averageWithCar': getAvg(usersWithCar.map(
(u) => u.age),
),
};
}
cleanConsole(5, companies);
console.log('---- EXAMPLE 5 --- ', f5(f4(companies)));