-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionalProgrammingPractice.js
135 lines (132 loc) · 2.39 KB
/
functionalProgrammingPractice.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// https://scotch.io/bar-talk/code-challenge-11-javascript-functional-programming
// ARRAY 1
const texasss = [
{
name: "Mike",
age: 23,
gender: "m",
us: false
},
{
name: "Liz",
age: 20,
gender: "f",
us: true
},
{
name: "Chris",
age: 102,
gender: "m",
us: true
},
{
name: "Chuloo",
age: 27,
gender: "m",
us: false
},
{
name: "Annie",
age: 30,
gender: "f",
us: true
}
];
// Part 1 - Find all users older than 24
console.log(texasss.filter(user => user.age > 24));
// Part 2 - Find the total age of all users
console.log(texasss.reduce((acc, user) => acc + parseInt(user.age), 0));
// Part 3 - List all female coders
console.log(texasss.filter(coder => coder.gender == "f"));
// ARRAY 2
const newieyork = [
{
name: "Michelle",
age: 19,
coder: true,
gender: "f",
us: true
},
{
name: "Sam",
age: 25,
coder: false,
gender: "m",
us: false
},
{
name: "Ivy",
age: 26,
coder: true,
gender: "f",
us: false
},
{
name: "Nick",
age: 32,
coder: true,
gender: "m",
us: true
},
{
name: "Jim Beglin",
age: 65,
coder: false,
gender: "m",
us: true
}
];
// Part 1 - List all users in US in ascending order
console.log(newieyork.filter(user => user.us).sort());
// Part 2 - Sort all users by age
console.log(newieyork.sort((a, b) => a.age - b.age));
// Part 3 - List all female coders
console.log(newieyork.filter(user => user.gender == "f"));
// ARRAY 3
const vegzas = [
{
name: "Charly",
age: 32,
coder: true,
gender: "m"
},
{
name: "Law",
age: 21,
coder: true,
gender: "m"
},
{
name: "Rosey",
age: 42,
coder: false,
gender: "f"
},
{
name: "Steph",
age: 18,
coder: true,
gender: "f"
},
{
name: "Jon",
age: 47,
coder: false,
gender: "m"
}
];
// Part 1 - Find the total age of male coders under 25
console.log(
vegzas
.filter(coder => coder.age < 25 && coder.gender == "m")
.reduce((acc, coder) => acc + coder.age, 0)
);
// Part 2 - List all male coders over 30
console.log(vegzas.filter(coder => coder.age > 30 && coder.gender == "m"));
// Part 3 - Find the total age of everyone in texasss, newieyork and vegzas combined.
console.log(
vegzas
.concat(newieyork)
.concat(texasss)
.reduce((acc, user) => acc + user.age, 0)
);