forked from CastonPursuit/Lab-Native-Array-Methods-Pt2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
346 lines (295 loc) · 9.76 KB
/
Copy pathindex.js
File metadata and controls
346 lines (295 loc) · 9.76 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Native Array Methods pt.2 continues with the same dataset: songs. All required functions and array methods (forEach, map, find, some/every, sort) are combined into a single file, each addressing a distinct problem.
*/
const exampleSongData = require("./data/songs");
// Do not change the line above.
// #1
/**
* Returns the titles of songs sorted alphabetically.
* @param {Object[]} songs - An array of songs.
* @returns {string[]} Sorted song titles.
*/
function getSortedTitles(songs) {
return songs.map(x => x.title).sort();
}
console.log(getSortedTitles(exampleSongData));
// #2
/**
* Returns the titles of all songs from a specified album.
* @param {Object[]} songs - An array of songs.
* @param {string} albumName - Name of the album.
* @returns {string[]} An array of song titles.
*/
function getSongsFromAlbum(songs, albumName) {
return songs.filter(x => x.album === albumName).map(x => x.title);
}
console.log(getSongsFromAlbum(exampleSongData, 'Bi-To Te-Pu'))
// #3
/**
* Categorizes and counts songs based on their runtime.
* @param {Object[]} songs - An array of songs.
* @returns {Object} An object with counts of short, medium, and long songs.
*/
function categorizeSongsByRuntime(songs) {
return {
longSongs: songs.filter(x => x.runtimeInSeconds > 220).length,
mediumSongs: songs.filter(x => x.runtimeInSeconds >= 180).length,
shortSongs: songs.filter(x => x.runtimeInSeconds < 180).length
};
};
// songs.forEach((song) => {
// if (song.runtimeInSeconds < 180) {
// runtimeCategorization.short++;
// } else if (song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300) {
// runtimeCategorization.medium++;
// } else if (song.runtimeInSeconds > 300) {
// runtimeCategorization.long++;
// }
// });
console.log(categorizeSongsByRuntime(exampleSongData))
// #4
/**
* Finds the album with the highest number of songs.
* @param {Object[]} songs - An array of songs.
* @returns {string} The name of the album with the most songs.
*/
function findAlbumWithMostSongs(songs) {
let highestSongCount = 0;
let albumWithMostSongs;
songs.reduce((songCountObj, currValue) => {
songCountObj[currValue.album] = (songCountObj[currValue.album] || 0) + 1;
if(songCountObj[currValue.album] > highestSongCount){
highestSongCount = songCountObj[currValue.album];
albumWithMostSongs = currValue.album
}
return songCountObj;
}, {});
return albumWithMostSongs;
}
console.log(findAlbumWithMostSongs(exampleSongData))
// #5
/**
* Returns details of the first song in a specific album.
* @param {Object[]} songs - An array of songs.
* @param {string} albumName - Name of the album.
* @returns {Object|null} First song object in the album or null.
*/
function getFirstSongInAlbum(songs, albumName) {
let getAlbumDetails = songs.find((x) => {
return {} === albumName
})
return getAlbumDetails;
}
// #6
/**
* Checks if there is at least one song longer than a specified runtime.
* @param {Object[]} songs - An array of songs.
* @param {number} runtime - The runtime to check against in seconds.
* @returns {boolean} True if there is at least one song longer than the runtime.
*/
function isThereLongSong(songs, runtime) {
Array.prototype.every.call(songs, (x) => typeof x.runtime < 400)
}
// #7
/**
* Transforms song data to show title and runtime in minutes.
* @param {Object[]} songs - An array of songs.
* @returns {Object[]} Array of song objects with runtime in minutes.
*/
function getSongsWithDurationInMinutes(songs) {}
// #8
/**
* Returns the album names in reverse alphabetical order.
* @param {Object[]} songs - An array of songs.
* @returns {string[]} Array of album names in reverse alphabetical order.
*/
function getAlbumsInReverseOrder(songs) {}
// #9
/**
* Returns a list of song titles that contain a specific word.
* @param {Object[]} songs - An array of songs.
* @param {string} word - The word to search for in song titles.
* @returns {string[]} An array of song titles containing the word.
*/
function songsWithWord(songs, word) {}
// #10
/**
* Returns the total runtime of songs by a specific artist.
* @param {Object[]} songs - An array of songs.
* @param {string} artistName - Name of the artist.
* @returns {number} Total runtime in seconds.
*/
function getTotalRuntimeOfArtist(songs, artistName) {}
// Problem #11
/**
* Prints artists who have more than one song in the list.
* @param {Object[]} songs - An array of songs.
*/
function printArtistsWithMultipleSongs(songs) {
let artistSongCount = {};
songs.forEach((song) => {
if(artistSongCount[song.artist]){
artistSongCount[song.artist]++;
}else{
artistSongCount[song.artist] = 1;
}
if(artistSongCount[song.artist] > 1){
console.log(song.artist);
}
return artistSongCount;
});
}
console.log(printArtistsWithMultipleSongs(exampleSongData))
// Problem #12
/**
* Logs the longest song title.
* @param {Object[]} songs - An array of songs.
*/
function printLongestSongTitle(songs) {}
// Problem #13
/**
* Sorts songs by artist name, then by song title alphabetically.
* @param {Object[]} songs - An array of songs.
* @returns {Object[]} Sorted array of songs.
*/
function sortSongsByArtistAndTitle(songs) {}
// Problem #14
/**
* Lists albums along with their total runtime.
* @param {Object[]} songs - An array of songs.
* @returns {Object} An object mapping each album to its total runtime.
*/
function listAlbumTotalRuntimes(songs) {
let albumTotalRuntimes = {};
songs.forEach(song => {
if(albumTotalRuntimes[song.album]){
albumTotalRuntimes[song.album] += song.runtimeInSeconds;
}else {
albumTotalRuntimes[song.album] = song.runtimeInSeconds
}
})
return albumTotalRuntimes;
}
console.log(listAlbumTotalRuntimes(exampleSongData))
// Problem #15
/**
* Finds the first song with a title starting with a specific letter.
* @param {Object[]} songs - An array of songs.
* @param {string} letter - The letter to search for.
* @returns {Object|null} The first song object that matches the criterion or null.
*/
function findFirstSongStartingWith(songs, letter) {
const firstSongWithLetter = songs.find(song => song.title[0] === letter || null);
return firstSongWithLetter
};
console.log(findFirstSongStartingWith(exampleSongData, "U") )
// Problem #16
/**
* Maps each artist to an array of their song titles.
* @param {Object[]} songs - An array of songs.
* @returns {Object} An object mapping each artist to an array of their song titles.
*/
function mapArtistsToSongs(songs) {
let artistMap ={};
songs.map(song => {
if(!artistMap.hasOwnProperty(song.artist)){
artistMap[song.artist] = [song.title]
}
else {
artistMap[song.artist].push(song.title);
}
})
return artistMap;
}
mapArtistsToSongs(exampleSongData)
// Problem #17
/**
* Finds the album with the longest average song runtime.
* @param {Object[]} songs - An array of songs.
* @returns {string} The name of the album with the longest average song runtime.
*/
function findAlbumWithLongestAverageRuntime(songs) {
const albumAverageRuntime = {};
let albumName = '';
let longestAvgRuntime = 0;
songs.forEach(song => {
if(!albumAverageRuntime[song.album]){
albumAverageRuntime[song.album] = {
totalRunTime: song.runtimeInSeconds,
songCount: 1,
avgRuntime: song.runtimeInSeconds,
};
} else {
albumAverageRuntime[song.album].totalRunTime += song.runtimeInSeconds;
albumAverageRuntime[song.album].songCount++;
albumAverageRuntime[song.album].avgRuntime = +(albumAverageRuntime[song.album].totalRunTime / albumAverageRuntime[song.album].songCount).toFixed(2)
}
if(albumAverageRuntime[song.album].avgRuntime > longestAvgRuntime){
longestAvgRuntime = albumAverageRuntime[song.album].avgRuntime;
albumName = song.album;
}
});
return albumName;
}
console.log(findAlbumWithLongestAverageRuntime(exampleSongData))
// Problem #18
/**
* Logs song titles sorted by their runtime.
* @param {Object[]} songs - An array of songs.
*/
function printSongsSortedByRuntime(songs) {
let songsSortedByRunTime = songs.sort((songA, songB) => songA.runtimeInSeconds - songB.runtimeInSeconds);
songsSortedByRunTime.forEach(song => console.log(song.title))
}
// Problem #19
/**
* Prints a summary of each album, including its name, total runtime, and number of songs.
* @param {Object[]} songs - An array of songs.
*/
function printAlbumSummaries(songs) {
let albumSummaries = {};
songs.forEach((song) => {
if(!albumSummaries[song.album]){
albumSummaries[song.album] = {
songCount: 1,
totalRunTime: song.runtimeInSeconds,
};
} else{
albumSummaries[song.album].songCount++;
albumSummaries[song.album].totalRunTime += song.runtimeInSeconds;
}
});
for(const summary in albumSummaries){
console.log(`${summary}: ${albumSummaries[summary].songCount} songs, Total Runtime: ${albumSummaries[summary].totalRunTime} seconds`);
}
}
console.log(printAlbumSummaries(exampleSongData))
// Problem #20
/**
* Finds the artist with the most songs in the list.
* @param {Object[]} songs - An array of songs.
* @returns {string} The name of the artist with the most songs.
*/
function findArtistWithMostSongs(songs) {}
module.exports = {
getSortedTitles,
getSongsFromAlbum,
categorizeSongsByRuntime,
findAlbumWithMostSongs,
getFirstSongInAlbum,
isThereLongSong,
getSongsWithDurationInMinutes,
getAlbumsInReverseOrder,
songsWithWord,
getTotalRuntimeOfArtist,
printArtistsWithMultipleSongs,
sortSongsByArtistAndTitle,
printLongestSongTitle,
listAlbumTotalRuntimes,
findFirstSongStartingWith,
mapArtistsToSongs,
findAlbumWithLongestAverageRuntime,
printSongsSortedByRuntime,
printAlbumSummaries,
findArtistWithMostSongs
};;