From 494d8ecda9d797f623d48adc86f5a8cc77ff1b29 Mon Sep 17 00:00:00 2001 From: nmatos Date: Thu, 11 Jan 2024 10:50:38 -0500 Subject: [PATCH 1/6] Did problems 1-4 usingfilter and reduce method --- index.js | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..8a7cfbd 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,11 @@ const exampleSongData = require("./data/songs"); * @param {Object[]} songs - An array of songs. * @returns {string[]} Sorted song titles. */ -function getSortedTitles(songs) {} +function getSortedTitles(songs) { + return songs.map(x => x.title).sort(); +} + +console.log(getSortedTitles(exampleSongData)); // #2 /** @@ -22,7 +26,10 @@ 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(x => x.album === albumName).map(x => x.title); +} +console.log(getSongsFromAlbum(exampleSongData, 'Bi-To Te-Pu')) // #3 /** @@ -30,7 +37,29 @@ function getSongsFromAlbum(songs, albumName) {} * @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) { + 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 /** @@ -38,7 +67,12 @@ 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 getAlbum = songs.reduce((albumName, song) => albumName.title > albumName.album ? albumName : song); + return getAlbum.album; +}; + +console.log(findAlbumWithMostSongs(exampleSongData)) // #5 /** From c0b78ceada37c1e3f56c626595badf8b7b73b444 Mon Sep 17 00:00:00 2001 From: nmatos Date: Thu, 11 Jan 2024 16:23:01 -0500 Subject: [PATCH 2/6] Solved problem #14 using the forEach method --- index.js | 50 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 8a7cfbd..b4cca03 100644 --- a/index.js +++ b/index.js @@ -68,9 +68,20 @@ console.log(categorizeSongsByRuntime(exampleSongData)) * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { - let getAlbum = songs.reduce((albumName, song) => albumName.title > albumName.album ? albumName : song); - return getAlbum.album; -}; + 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)) @@ -81,7 +92,16 @@ console.log(findAlbumWithMostSongs(exampleSongData)) * @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) { + let getAlbumDetails = songs.find((x) => { + return {} === albumName + }) + return getAlbumDetails; + +} + + + // #6 /** @@ -90,7 +110,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) { + Array.prototype.every.call(songs, (x) => typeof x.runtime < 400) +} // #7 /** @@ -154,10 +176,24 @@ 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) { + 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. From a313f42d7bf835aeea64288496354cc83a6b7732 Mon Sep 17 00:00:00 2001 From: nmatos Date: Thu, 11 Jan 2024 16:44:09 -0500 Subject: [PATCH 3/6] Solved problem #11 using forEach --- index.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b4cca03..2abd0cb 100644 --- a/index.js +++ b/index.js @@ -153,8 +153,21 @@ 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) {} +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); + } + }); + } + console.log(printArtistsWithMultipleSongs(exampleSongData)) // Problem #12 /** * Logs the longest song title. From e61f89b0d9463d82bfa383e51ffe4f928e4e97d6 Mon Sep 17 00:00:00 2001 From: nmatos Date: Fri, 12 Jan 2024 10:40:28 -0500 Subject: [PATCH 4/6] Did problem #15 using find filter --- index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2abd0cb..65ffe3d 100644 --- a/index.js +++ b/index.js @@ -164,6 +164,7 @@ function printArtistsWithMultipleSongs(songs) { if(artistSongCount[song.artist] > 1){ console.log(song.artist); } + return artistSongCount; }); } @@ -212,7 +213,12 @@ console.log(listAlbumTotalRuntimes(exampleSongData)) * @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) { + const firstSongWithLetter = songs.find(song => song.title[0] === letter); + return firstSongWithLetter +}; + +console.log(findFirstSongStartingWith(exampleSongData, "U") ) // Problem #16 /** From 98e9ca95a1c0f403ac8215cd832d4ece53690f6d Mon Sep 17 00:00:00 2001 From: nmatos Date: Fri, 12 Jan 2024 12:02:39 -0500 Subject: [PATCH 5/6] Did problems 16 and 18 using map and sort method --- index.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 65ffe3d..f63dbac 100644 --- a/index.js +++ b/index.js @@ -214,7 +214,7 @@ console.log(listAlbumTotalRuntimes(exampleSongData)) * @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); + const firstSongWithLetter = songs.find(song => song.title[0] === letter || null); return firstSongWithLetter }; @@ -226,7 +226,21 @@ console.log(findFirstSongStartingWith(exampleSongData, "U") ) * @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) { +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 /** @@ -241,7 +255,11 @@ function findAlbumWithLongestAverageRuntime(songs) {} * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { + let songsSortedByRunTime = songs.sort((songA, songB) => songA.runtimeInSeconds - songB.runtimeInSeconds); + songsSortedByRunTime.forEach(song => console.log(song.title)) + +} // Problem #19 /** From 7bf32c795499ed6febc53a83cecba8cd4e468bce Mon Sep 17 00:00:00 2001 From: nmatos Date: Tue, 16 Jan 2024 07:32:25 -0500 Subject: [PATCH 6/6] Solved 2 problems --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f63dbac..dc937f9 100644 --- a/index.js +++ b/index.js @@ -248,7 +248,35 @@ mapArtistsToSongs(exampleSongData) * @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 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 /** @@ -266,7 +294,24 @@ function printSongsSortedByRuntime(songs) { * 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 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 /**