-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-filter-reduce-exercise.html
More file actions
117 lines (90 loc) · 2.94 KB
/
map-filter-reduce-exercise.html
File metadata and controls
117 lines (90 loc) · 2.94 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Map Filter Reduce</title>
</head>
<body>
<script>
const users = [
{
id: 1,
name: 'ryan',
email: 'ryan@codeup.com',
languages: ['clojure', 'javascript'],
yearsOfExperience: 5
},
{
id: 2,
name: 'luis',
email: 'luis@codeup.com',
languages: ['java', 'scala', 'php'],
yearsOfExperience: 6
},
{
id: 3,
name: 'zach',
email: 'zach@codeup.com',
languages: ['javascript', 'bash'],
yearsOfExperience: 7
},
{
id: 4,
name: 'fernando',
email: 'fernando@codeup.com',
languages: ['java', 'php', 'sql'],
yearsOfExperience: 8
},
{
id: 5,
name: 'justin',
email: 'justin@codeup.com',
languages: ['html', 'css', 'javascript', 'php'],
yearsOfExperience: 9
}
];
// Use .filter to create an array of user objects where each user object has at least 3 languages in the languages array.
let atLeastThreeLanguages = users.filter((user) => {
return user.languages.length >= 3;
})
console.log(atLeastThreeLanguages);
// Use .map to create an array of strings where each element is a user's email address
let usersEmails = users.map((user) => {
return user.email;
})
console.log(usersEmails);
// Use .reduce to get the total years of experience from the list of users. Once you get the total of years you can use the result to calculate the average.
let totalYears = users.reduce((accum, currentUserObj) => {
return accum += currentUserObj.yearsOfExperience;
}, 0)
console.log(totalYears);
// Use .reduce to get the longest email from the list of users.
let longestEmail = users.reduce((accum, currentUserObj) => {
if(currentUserObj.email.length > accum.length){
accum = currentUserObj.email;
return accum;
}
return accum;
}, "")
console.log(longestEmail);
// Use .reduce to get the list of user's names in a single string. Example: Your instructors are: ryan, luis, zach, fernando, justin.
let listOfNames = users.reduce((accum, currentUserObj, index) => {
if((users.length - 1) === index){
return accum += currentUserObj.name + "."
}
return accum += currentUserObj.name + ", ";
}, "Your instructors are: ");
console.log(listOfNames);
// bonus
let usersLanguages = users.reduce((accumulator, current) => {
current.languages.forEach((language) => {
if(!accumulator.includes(language)){
accumulator.push(language);
}
})
return accumulator;
}, [])
console.log(usersLanguages);
</script>
</body>
</html>