From 96bf29c98d66b9fe7321cb29e4899940c6c3b61e Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 15:25:20 -0500 Subject: [PATCH 1/7] Solve problems 1 through 20 --- index.js | 247 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 218 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..8dac5bb 100644 --- a/index.js +++ b/index.js @@ -1,19 +1,22 @@ -/* -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. -*/ - +/**********************************************************************************/ +/* 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 songs = require("./data/songs"); 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) {} +function getSortedTitles(songs) { + return songs.map((song) => song.title).sort(); +} // #2 /** @@ -22,15 +25,30 @@ function getSortedTitles(songs) {} * @param {string} albumName - Name of the album. * @returns {string[]} An array of song titles. */ -function getSongsFromAlbum(songs, albumName) {} +function getSongsFromAlbum(songs, albumName) { + return songs + .filter((song) => song.album == albumName) + .map((song) => song.title); +} -// #3 +// #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) {} +function categorizeSongsByRuntime(songs) { + const short = songs.filter((song) => song.runtimeInSeconds < 180); + const medium = songs.filter( + (song) => song.runtimeInSeconds >= 180 && song.runtimeInSeconds < 300 + ); + const long = songs.filter((song) => song.runtimeInSeconds >= 300); + return { + shortSongs: short.length, + mediumSongs: medium.length, + longSongs: long.length, + }; +} // #4 /** @@ -38,7 +56,27 @@ function categorizeSongsByRuntime(songs) {} * @param {Object[]} songs - An array of songs. * @returns {string} The name of the album with the most songs. */ -function findAlbumWithMostSongs(songs) {} +function findAlbumWithMostSongs(songs) { + let albunsSongs = {}; + songs.forEach((song) => { + if (albunsSongs.hasOwnProperty(song.album)) { + albunsSongs[song.album]++; + } else { + albunsSongs[song.album] = 1; + } + }); + + let albumWithMostSongs = ""; + for (const album in albunsSongs) { + if (albumWithMostSongs == "") { + albumWithMostSongs = album; + } else if (albunsSongs[albumWithMostSongs] < albunsSongs[album]) { + albumWithMostSongs = album; + } + } + + return albumWithMostSongs; +} // #5 /** @@ -47,7 +85,9 @@ function findAlbumWithMostSongs(songs) {} * @param {string} albumName - Name of the album. * @returns {Object|null} First song object in the album or null. */ -function getFirstSongInAlbum(songs, albumName) {} +function getFirstSongInAlbum(songs, albumName) { + return songs.filter((song) => song.album == albumName)[0]; +} // #6 /** @@ -56,7 +96,9 @@ function getFirstSongInAlbum(songs, albumName) {} * @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) {} +function isThereLongSong(songs, runtime) { + return songs.some((song) => song.runtimeInSeconds > runtime); +} // #7 /** @@ -64,7 +106,12 @@ function isThereLongSong(songs, runtime) {} * @param {Object[]} songs - An array of songs. * @returns {Object[]} Array of song objects with runtime in minutes. */ -function getSongsWithDurationInMinutes(songs) {} +function getSongsWithDurationInMinutes(songs) { + return songs.map((song) => { + song.durationInMinutes = song.runtimeInSeconds / 60; + return song; + }); +} // #8 /** @@ -72,7 +119,21 @@ function getSongsWithDurationInMinutes(songs) {} * @param {Object[]} songs - An array of songs. * @returns {string[]} Array of album names in reverse alphabetical order. */ -function getAlbumsInReverseOrder(songs) {} +function getAlbumsInReverseOrder(songs) { + const albuns = []; + songs.forEach((song) => { + if (!albuns.includes(song.album)) { + albuns.push(song.album); + } + }); + return albuns.sort((a, b) => { + if (a < b) { + return 1; + } else { + return -1; + } + }); +} // #9 /** @@ -81,7 +142,15 @@ function getAlbumsInReverseOrder(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) {} +function songsWithWord(songs, word) { + return songs + .map((song) => song.title) + .filter((song) => { + if (song.includes(word)) { + return song; + } + }); +} // #10 /** @@ -90,21 +159,47 @@ function songsWithWord(songs, word) {} * @param {string} artistName - Name of the artist. * @returns {number} Total runtime in seconds. */ -function getTotalRuntimeOfArtist(songs, artistName) {} +function getTotalRuntimeOfArtist(songs, artistName) { + return songs + .filter((song) => song.artist == artistName) + .reduce((totalRuntime, song) => (totalRuntime += song.runtimeInSeconds), 0); +} // Problem #11 /** * Prints artists who have more than one song in the list. * @param {Object[]} songs - An array of songs. */ -function printArtistsWithMultipleSongs(songs) {} +function printArtistsWithMultipleSongs(songs) { + const artistSongs = {}; + songs.forEach((song) => { + if (artistSongs.hasOwnProperty(song.artist)) { + artistSongs[song.artist].push(song.title); + } else { + artistSongs[song.artist] = [song.title]; + } + }); + for (const artis in artistSongs) { + if (artistSongs[artis].length > 1) { + console.log(artis); + } + } +} // Problem #12 /** * Logs the longest song title. * @param {Object[]} songs - An array of songs. */ -function printLongestSongTitle(songs) {} +function printLongestSongTitle(songs) { + let longestSongTitle = ""; + songs.forEach((song) => { + if (song.title.length > longestSongTitle.length) { + longestSongTitle = song.title; + } + }); + console.log(longestSongTitle); +} // Problem #13 /** @@ -112,7 +207,13 @@ function printLongestSongTitle(songs) {} * @param {Object[]} songs - An array of songs. * @returns {Object[]} Sorted array of songs. */ -function sortSongsByArtistAndTitle(songs) {} +function sortSongsByArtistAndTitle(songs) { + return songs.sort((songA, songB) => { + const artistCompare = songA.artist.localeCompare(songB.artist); + const titleCompare = songA.title.localeCompare(songB.title); + return artistCompare || titleCompare; + }); +} // Problem #14 /** @@ -120,7 +221,17 @@ function sortSongsByArtistAndTitle(songs) {} * @param {Object[]} songs - An array of songs. * @returns {Object} An object mapping each album to its total runtime. */ -function listAlbumTotalRuntimes(songs) {} +function listAlbumTotalRuntimes(songs) { + const albumTotalRuntimes = {}; + songs.forEach((song) => { + if (albumTotalRuntimes.hasOwnProperty(song.album)) { + albumTotalRuntimes[song.album] += song.runtimeInSeconds; + } else { + albumTotalRuntimes[song.album] = song.runtimeInSeconds; + } + }); + return albumTotalRuntimes; +} // Problem #15 /** @@ -129,7 +240,9 @@ function listAlbumTotalRuntimes(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) {} +function findFirstSongStartingWith(songs, letter) { + return songs.find((song) => song.title[0] == letter.toUpperCase()); +} // Problem #16 /** @@ -137,7 +250,17 @@ function findFirstSongStartingWith(songs, letter) {} * @param {Object[]} songs - An array of songs. * @returns {Object} An object mapping each artist to an array of their song titles. */ -function mapArtistsToSongs(songs) {} +function mapArtistsToSongs(songs) { + const artistSongs = {}; + songs.forEach((song) => { + if (artistSongs.hasOwnProperty(song.artist)) { + artistSongs[song.artist].push(song.title); + } else { + artistSongs[song.artist] = [song.title]; + } + }); + return artistSongs; +} // Problem #17 /** @@ -145,21 +268,65 @@ function mapArtistsToSongs(songs) {} * @param {Object[]} songs - An array of songs. * @returns {string} The name of the album with the longest average song runtime. */ -function findAlbumWithLongestAverageRuntime(songs) {} +function findAlbumWithLongestAverageRuntime(songs) { + const outputAlbum = { + name: "", + avgRuntime: 0, + }; + let albuns = songs.map((song) => song.album); + albuns.filter((album, idx) => albuns.indexOf(album) === idx); + albuns.forEach((albun) => { + const albumSongs = songs.filter((song) => song.album == albun); + const totalRuntime = albumSongs.reduce( + (acc, song) => acc + song.runtimeInSeconds, + 0 + ); + const avgRuntime = totalRuntime / albumSongs.length; + if (outputAlbum.avgRuntime <= avgRuntime) { + outputAlbum.name = albun; + outputAlbum.avgRuntime = avgRuntime; + } + }); + return outputAlbum.name; +} // Problem #18 /** * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { + return songs + .sort((songA, songB) => { + if (songA.runtimeInSeconds <= songB.runtimeInSeconds) { + return -1; + } else { + return 1; + } + }) + .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) {} +function printAlbumSummaries(songs) { + let albuns = songs.map((song) => song.album); + albuns = albuns.filter((album, idx) => albuns.indexOf(album) === idx); + return albuns.forEach((album) => { + const albumSongs = songs.filter((song) => song.album == album); + const numSongs = albumSongs.length; + const totalRuntime = albumSongs.reduce( + (acc, song) => acc + song.runtimeInSeconds, + 0 + ); + console.log( + `${album}: ${numSongs} songs, Total Runtime: ${totalRuntime} seconds` + ); + }); +} // Problem #20 /** @@ -167,13 +334,35 @@ function printAlbumSummaries(songs) {} * @param {Object[]} songs - An array of songs. * @returns {string} The name of the artist with the most songs. */ -function findArtistWithMostSongs(songs) {} +function findArtistWithMostSongs(songs) { + const artistWithMostSongs = { + name: "", + numSongs: 0, + }; + + const artistSongs = {}; + songs.forEach((song) => { + if (artistSongs.hasOwnProperty(song.artist)) { + artistSongs[song.artist].push(song.title); + } else { + artistSongs[song.artist] = [song.title]; + } + }); + for (const artist in artistSongs) { + const numSongs = artistSongs[artist].length; + if (artistWithMostSongs.numSongs < numSongs) { + artistWithMostSongs.name = artist; + artistWithMostSongs.numSongs = numSongs; + } + } + return artistWithMostSongs.name; +} module.exports = { getSortedTitles, getSongsFromAlbum, - categorizeSongsByRuntime, + categorizeSongsByRuntime, findAlbumWithMostSongs, getFirstSongInAlbum, isThereLongSong, @@ -190,5 +379,5 @@ module.exports = { findAlbumWithLongestAverageRuntime, printSongsSortedByRuntime, printAlbumSummaries, - findArtistWithMostSongs -};; + findArtistWithMostSongs, +}; From 727f408fad1a6a30bfd02970a55495993fb42cb6 Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 15:32:49 -0500 Subject: [PATCH 2/7] Refactor problem 4 --- index.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 8dac5bb..21128ae 100644 --- a/index.js +++ b/index.js @@ -57,25 +57,20 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { - let albunsSongs = {}; - songs.forEach((song) => { - if (albunsSongs.hasOwnProperty(song.album)) { - albunsSongs[song.album]++; - } else { - albunsSongs[song.album] = 1; + const outputAlbum = { + name: "", + numSongs: 0, + }; + let albuns = songs.map((song) => song.album); + albuns = albuns.filter((album, idx) => albuns.indexOf(album) === idx); + albuns.forEach((album) => { + const numSongs = songs.filter((song) => song.album == album).length; + if (outputAlbum.numSongs <= numSongs) { + outputAlbum.name = album; + outputAlbum.numSongs = numSongs; } }); - - let albumWithMostSongs = ""; - for (const album in albunsSongs) { - if (albumWithMostSongs == "") { - albumWithMostSongs = album; - } else if (albunsSongs[albumWithMostSongs] < albunsSongs[album]) { - albumWithMostSongs = album; - } - } - - return albumWithMostSongs; + return outputAlbum.name; } // #5 From 78c54cb789f613c1bf22eefa75bbb0271b27e345 Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 15:45:55 -0500 Subject: [PATCH 3/7] Refactor problem 11 --- index.js | 57 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index 21128ae..a7c2a11 100644 --- a/index.js +++ b/index.js @@ -62,14 +62,15 @@ function findAlbumWithMostSongs(songs) { numSongs: 0, }; let albuns = songs.map((song) => song.album); - albuns = albuns.filter((album, idx) => albuns.indexOf(album) === idx); - albuns.forEach((album) => { - const numSongs = songs.filter((song) => song.album == album).length; - if (outputAlbum.numSongs <= numSongs) { - outputAlbum.name = album; - outputAlbum.numSongs = numSongs; - } - }); + albuns + .filter((album, idx) => albuns.indexOf(album) === idx) + .forEach((album) => { + const numSongs = songs.filter((song) => song.album == album).length; + if (outputAlbum.numSongs <= numSongs) { + outputAlbum.name = album; + outputAlbum.numSongs = numSongs; + } + }); return outputAlbum.name; } @@ -165,22 +166,36 @@ function getTotalRuntimeOfArtist(songs, artistName) { * Prints artists who have more than one song in the list. * @param {Object[]} songs - An array of songs. */ +// function printArtistsWithMultipleSongs(songs) { +// const artistSongs = {}; +// songs.forEach((song) => { +// if (artistSongs.hasOwnProperty(song.artist)) { +// artistSongs[song.artist].push(song.title); +// } else { +// artistSongs[song.artist] = [song.title]; +// } +// }); +// for (const artis in artistSongs) { +// if (artistSongs[artis].length > 1) { +// console.log(artis); +// } +// } +// } function printArtistsWithMultipleSongs(songs) { - const artistSongs = {}; - songs.forEach((song) => { - if (artistSongs.hasOwnProperty(song.artist)) { - artistSongs[song.artist].push(song.title); - } else { - artistSongs[song.artist] = [song.title]; - } - }); - for (const artis in artistSongs) { - if (artistSongs[artis].length > 1) { - console.log(artis); - } - } + let artists = songs.map((song) => song.artist); + // artists = artists.filter((artist, idx) => artists.indexOf(artist) === idx); + artists + .filter((artist, idx) => artists.indexOf(artist) === idx) + .forEach((artist) => { + const artistSongs = songs.filter((song) => song.artist == artist); + if (artistSongs.length > 1) { + console.log(artist); + } + }); } +printArtistsWithMultipleSongs(exampleSongData); + // Problem #12 /** * Logs the longest song title. From 805f7a6d7890bbc256553a1dc62829bc72e9407e Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 15:47:11 -0500 Subject: [PATCH 4/7] Refactor problem 17 --- index.js | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index a7c2a11..91c5dd7 100644 --- a/index.js +++ b/index.js @@ -166,24 +166,8 @@ function getTotalRuntimeOfArtist(songs, artistName) { * Prints artists who have more than one song in the list. * @param {Object[]} songs - An array of songs. */ -// function printArtistsWithMultipleSongs(songs) { -// const artistSongs = {}; -// songs.forEach((song) => { -// if (artistSongs.hasOwnProperty(song.artist)) { -// artistSongs[song.artist].push(song.title); -// } else { -// artistSongs[song.artist] = [song.title]; -// } -// }); -// for (const artis in artistSongs) { -// if (artistSongs[artis].length > 1) { -// console.log(artis); -// } -// } -// } function printArtistsWithMultipleSongs(songs) { let artists = songs.map((song) => song.artist); - // artists = artists.filter((artist, idx) => artists.indexOf(artist) === idx); artists .filter((artist, idx) => artists.indexOf(artist) === idx) .forEach((artist) => { @@ -284,19 +268,20 @@ function findAlbumWithLongestAverageRuntime(songs) { avgRuntime: 0, }; let albuns = songs.map((song) => song.album); - albuns.filter((album, idx) => albuns.indexOf(album) === idx); - albuns.forEach((albun) => { - const albumSongs = songs.filter((song) => song.album == albun); - const totalRuntime = albumSongs.reduce( - (acc, song) => acc + song.runtimeInSeconds, - 0 - ); - const avgRuntime = totalRuntime / albumSongs.length; - if (outputAlbum.avgRuntime <= avgRuntime) { - outputAlbum.name = albun; - outputAlbum.avgRuntime = avgRuntime; - } - }); + albuns + .filter((album, idx) => albuns.indexOf(album) === idx) + .forEach((albun) => { + const albumSongs = songs.filter((song) => song.album == albun); + const totalRuntime = albumSongs.reduce( + (acc, song) => acc + song.runtimeInSeconds, + 0 + ); + const avgRuntime = totalRuntime / albumSongs.length; + if (outputAlbum.avgRuntime <= avgRuntime) { + outputAlbum.name = albun; + outputAlbum.avgRuntime = avgRuntime; + } + }); return outputAlbum.name; } From 3359d91a548b64885c77a2232c4b4ccdc4b50fa8 Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 15:48:13 -0500 Subject: [PATCH 5/7] Refactor problem 19 --- index.js | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 91c5dd7..bbc01af 100644 --- a/index.js +++ b/index.js @@ -309,18 +309,19 @@ function printSongsSortedByRuntime(songs) { */ function printAlbumSummaries(songs) { let albuns = songs.map((song) => song.album); - albuns = albuns.filter((album, idx) => albuns.indexOf(album) === idx); - return albuns.forEach((album) => { - const albumSongs = songs.filter((song) => song.album == album); - const numSongs = albumSongs.length; - const totalRuntime = albumSongs.reduce( - (acc, song) => acc + song.runtimeInSeconds, - 0 - ); - console.log( - `${album}: ${numSongs} songs, Total Runtime: ${totalRuntime} seconds` - ); - }); + albuns = albuns + .filter((album, idx) => albuns.indexOf(album) === idx) + .forEach((album) => { + const albumSongs = songs.filter((song) => song.album == album); + const numSongs = albumSongs.length; + const totalRuntime = albumSongs.reduce( + (acc, song) => acc + song.runtimeInSeconds, + 0 + ); + console.log( + `${album}: ${numSongs} songs, Total Runtime: ${totalRuntime} seconds` + ); + }); } // Problem #20 From 1a325488de318eb0eed00b62c8ec1fdeebd0381a Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 15:59:14 -0500 Subject: [PATCH 6/7] Refactor problem 20 --- index.js | 60 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/index.js b/index.js index bbc01af..0bead89 100644 --- a/index.js +++ b/index.js @@ -178,8 +178,6 @@ function printArtistsWithMultipleSongs(songs) { }); } -printArtistsWithMultipleSongs(exampleSongData); - // Problem #12 /** * Logs the longest song title. @@ -330,29 +328,49 @@ function printAlbumSummaries(songs) { * @param {Object[]} songs - An array of songs. * @returns {string} The name of the artist with the most songs. */ +// function findArtistWithMostSongs(songs) { +// const artistWithMostSongs = { +// name: "", +// numSongs: 0, +// }; + +// const artistSongs = {}; +// songs.forEach((song) => { +// if (artistSongs.hasOwnProperty(song.artist)) { +// artistSongs[song.artist].push(song.title); +// } else { +// artistSongs[song.artist] = [song.title]; +// } +// }); + +// for (const artist in artistSongs) { +// const numSongs = artistSongs[artist].length; +// if (artistWithMostSongs.numSongs < numSongs) { +// artistWithMostSongs.name = artist; +// artistWithMostSongs.numSongs = numSongs; +// } +// } +// return artistWithMostSongs.name; +// } +// console.log(findArtistWithMostSongs(exampleSongData)); + function findArtistWithMostSongs(songs) { - const artistWithMostSongs = { + const outputArtist = { name: "", - numSongs: 0, + numSongs: "", }; + const artists = songs.map((song) => song.artist); + artists + .filter((artist, idx) => artists.indexOf(artist) === idx) + .forEach((artist) => { + const numSongs = songs.filter((song) => song.artist == artist).length; - const artistSongs = {}; - songs.forEach((song) => { - if (artistSongs.hasOwnProperty(song.artist)) { - artistSongs[song.artist].push(song.title); - } else { - artistSongs[song.artist] = [song.title]; - } - }); - - for (const artist in artistSongs) { - const numSongs = artistSongs[artist].length; - if (artistWithMostSongs.numSongs < numSongs) { - artistWithMostSongs.name = artist; - artistWithMostSongs.numSongs = numSongs; - } - } - return artistWithMostSongs.name; + if (outputArtist.numSongs < numSongs) { + outputArtist.name = artist; + outputArtist.numSongs = numSongs; + } + }); + return outputArtist.name; } module.exports = { From 87f4385d3650c0fc819f6b35f37b739281ce36ba Mon Sep 17 00:00:00 2001 From: manoelteixeira Date: Thu, 11 Jan 2024 16:03:35 -0500 Subject: [PATCH 7/7] Remove unnecessary code --- index.js | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/index.js b/index.js index 0bead89..504c8e7 100644 --- a/index.js +++ b/index.js @@ -328,31 +328,6 @@ function printAlbumSummaries(songs) { * @param {Object[]} songs - An array of songs. * @returns {string} The name of the artist with the most songs. */ -// function findArtistWithMostSongs(songs) { -// const artistWithMostSongs = { -// name: "", -// numSongs: 0, -// }; - -// const artistSongs = {}; -// songs.forEach((song) => { -// if (artistSongs.hasOwnProperty(song.artist)) { -// artistSongs[song.artist].push(song.title); -// } else { -// artistSongs[song.artist] = [song.title]; -// } -// }); - -// for (const artist in artistSongs) { -// const numSongs = artistSongs[artist].length; -// if (artistWithMostSongs.numSongs < numSongs) { -// artistWithMostSongs.name = artist; -// artistWithMostSongs.numSongs = numSongs; -// } -// } -// return artistWithMostSongs.name; -// } -// console.log(findArtistWithMostSongs(exampleSongData)); function findArtistWithMostSongs(songs) { const outputArtist = {