-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-filter-reduce-lecture.html
More file actions
231 lines (156 loc) · 6.56 KB
/
map-filter-reduce-lecture.html
File metadata and controls
231 lines (156 loc) · 6.56 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Functional Methods</title>
</head>
<body>
<h1>Functional Methods</h1>
<main>
<section>
<h2>My Cats Names </h2>
<ul id="myCats"></ul>
</section>
<section>
<h2>Cat with Short Names</h2>
<ul id="shortNamesCats"></ul>
</section>
</main>
<script>
"use strict";
// Array of numbers someone likes for some reason
const favoriteNumbers = [11, 17, 15, 12, 100, 7, 1, 3, 9, 50,52, 44, 13, 19];
const myCats = ['Little One', "Mao Mao", "Lizzy"]
const arrayOfTVShows = [
{
title: "The Office",
seasons: 9,
imdbScore: 9,
firstEpisodeYear: 2005,
characters: ["Micheal", "Pam", "Jim", "Andy"]
},
{
title: "Game of Thrones",
seasons: 8,
imdbScore: 9.3,
firstEpisodeYear: 2011,
characters: ["Jon", "Arya", "Sansa", "Tyrion"]
},
{
title: "The Good Place",
seasons: 4,
imdbScore: 8.2,
firstEpisodeYear: 2016,
characters: ["Janet", "Chidi", "Elenor", "Jason", "Tahani"]
},
{
title: "Breaking Bad",
seasons: 5,
imdbScore: 9.5,
firstEpisodeYear: 2008,
characters: ["Walter", "Jesse", "Skylar"]
},
]
// MAP
// TODO: double each number in the favorite numbers array.
const doubleNumbersArray = favoriteNumbers.map((number) => number*2);
console.log("doubleNumbersArray:", doubleNumbersArray)
console.log("favoriteNumbers:", favoriteNumbers)
// You could use forEAch to solve the same problem in another way. Although the syntax is longer in many cases
const forEachExample = [];
favoriteNumbers.forEach((element) => {
forEachExample.push(element *2)
})
console.log("forEachExample:", forEachExample)
const uppercaseCats = myCats.map((element) => element.toUpperCase())
console.log("uppercaseCats:", uppercaseCats)
// This is easily reusable in other map functions
const mapElementToLI = (element) => `<li>${element}</li>`
const uppercaseCatsHTML = uppercaseCats.map(mapElementToLI)
console.log("uppercaseCatsHTML:", uppercaseCatsHTML)
document.getElementById("myCats").innerHTML = uppercaseCatsHTML.join("")
//TODO: create a headline that could be accessed later. using the title, seasons and imdbScore
const mapHeadlineToArray = (show) => {
const { title, seasons, imdbScore } = show
return {
...show, // spread operator, allows you to get all the properties from show and display them in resulted object
headline: `${title} (${seasons} seasons) has an average IMDB score of ${imdbScore}`
}
}
const headlineTVShows = arrayOfTVShows.map(mapHeadlineToArray);
console.log("headlineTVShows:", headlineTVShows)
// FILTER
// TODO: Get only the odd numbers from the favorite numbers array
const oddFavorites = favoriteNumbers.filter((number) => number % 2)
console.log("oddFavorites:", oddFavorites)
// TODO: From the cats array I want to only get the cats that have a name of 7 or
// less characters.
// You can separate the callback function from the filter if needed for readability or otherwise
const filterCatsUnder7Characters = (cat) => cat.length < 10
const catsWithShortNames = myCats.filter(filterCatsUnder7Characters);
console.log("catsWithShortNames:", catsWithShortNames)
// TODO: Combine the tools!
// TODO: take the cats array that you returned from the filter above and map it to the page.
const catsWithShortNamesHTML = myCats.filter(filterCatsUnder7Characters).map(mapElementToLI)
console.log("catsWithShortNamesHTML:", catsWithShortNamesHTML)
document.getElementById("shortNamesCats").innerHTML = catsWithShortNamesHTML.join("");
// TODO: Filter the movies array such that it would return items 9 or over on the
// imdb rating scale and the show is created after or equal to 2008
// This item filters on two values, you can of course do more if you needed it
const bestShow = arrayOfTVShows.filter((show) => show.imdbScore >= 9 && show.firstEpisodeYear >= 2008)
console.log("bestShow:", bestShow)
// REDUCE
// TODO: Get the sum of all favorite numbers
// const favoriteSum = favoriteNumbers.reduce((previousValue, currentValue) => previousValue+currentValue)
//
//
// console.log("favoriteSum:", favoriteSum)
const favoriteSum = favoriteNumbers.reduce((previousValue, currentValue, index) => {
// console.log("index :", index);
return previousValue+currentValue
}, 0);
console.log("favoriteSum:", favoriteSum)
//TODO:
const title = 'Full Cats HTML'
const fullCatsHTML = myCats
.map(mapElementToLI)
.reduce((previousValue, currentValue) => previousValue + currentValue, `<h2>${title}</h2><ul>`) + "</ul>"
console.log("fullCatsHTML:", fullCatsHTML)
document.getElementsByTagName("body")[0].innerHTML += fullCatsHTML
// TODO: Create a rollup report that contians the total # of TV Shows, the average IMDB SCORE,
// and the all the starting years for the shows
const initialValue = {
totalTVShows: 0,
avgIMDBScore: 0,
startingYears: []
}
const getTVShowsReport = (previousValue, currentValue, index) => {
let {totalTVShows, avgIMDBScore, startingYears} = previousValue;
avgIMDBScore = ((avgIMDBScore * index ) + currentValue.imdbScore) / (index + 1);
startingYears.push(currentValue.firstEpisodeYear)
return {
totalTVShows: totalTVShows + 1,
avgIMDBScore,
startingYears
}
}
const tvShowReport = arrayOfTVShows.reduce(getTVShowsReport,initialValue);
console.log("tvShowReport:", tvShowReport);
const report = [tvShowReport]
.map((item) => {
console.log("item :", item);
const yearsMap = (item, index, array) => `<span>${item}</span>${(index < (array.length - 1)) ? "," : "." } `
return `
<h2>TV Show Report</h2>
<div>
<div>Total TV shows: ${item.totalTVShows}</div>
<div>Avg IMDB Score: ${item.avgIMDBScore}</div>
<div>Starting years: ${item.startingYears.map(yearsMap).join("")}</div>
</div>
`
})
console.log("report:", report)
document.getElementsByTagName("body")[0].innerHTML += report
</script>
</body>
</html>