From db186dd52a17269589097a67c494a706aadeb3ea Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Mon, 8 Jan 2024 13:33:54 -0500 Subject: [PATCH 01/15] Solved getSortedTitles() using .map() and .sort() --- index.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..85e4d68 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 /** @@ -24,7 +28,7 @@ function getSortedTitles(songs) {} */ function getSongsFromAlbum(songs, albumName) {} -// #3 +// #3 /** * Categorizes and counts songs based on their runtime. * @param {Object[]} songs - An array of songs. @@ -173,7 +177,7 @@ function findArtistWithMostSongs(songs) {} module.exports = { getSortedTitles, getSongsFromAlbum, - categorizeSongsByRuntime, + categorizeSongsByRuntime, findAlbumWithMostSongs, getFirstSongInAlbum, isThereLongSong, From ee600901e0f28c9d90572455bc989b66b869f061 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Tue, 9 Jan 2024 21:08:50 -0500 Subject: [PATCH 02/15] Solve problem #2 using .filter() and .map() --- README.md | 2 +- index.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e15a251..8d1fbf0 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# 🚀 Lab: Native Array Methods pt.2 +****# 🚀 Lab: Native Array Methods pt.2 This lab is an opportunity to enhance your JavaScript skills through hands-on practice with array methods `forEach`, `map`, `find`, `some/every`, and `sort()`. diff --git a/index.js b/index.js index 85e4d68..5419568 100644 --- a/index.js +++ b/index.js @@ -26,7 +26,9 @@ console.log(getSortedTitles(exampleSongData)) * @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) +} // #3 /** @@ -34,7 +36,8 @@ 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) { +} // #4 /** From 0a571dfbe0b1b75eef38a574c5ad1b17371ad57f Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Tue, 9 Jan 2024 21:43:17 -0500 Subject: [PATCH 03/15] Solve problem #3 using .filter() --- index.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5419568..c90933f 100644 --- a/index.js +++ b/index.js @@ -36,7 +36,19 @@ function getSongsFromAlbum(songs, albumName) { * @param {Object[]} songs - An array of songs. * @returns {Object} An object with counts of short, medium, and long songs. */ + +// Expected: {"longSongs": 0, "mediumSongs": 12, "shortSongs": 11} + function categorizeSongsByRuntime(songs) { + const longSongs = songs.filter(x => x.runtimeInSeconds > 220).length; + const mediumSongs = songs.filter(x => x.runtimeInSeconds >= 180).length; + const shortSongs = songs.filter(x => x.runtimeInSeconds < 180).length; + + return { + longSongs: longSongs, + mediumSongs: mediumSongs, + shortSongs : shortSongs + } } // #4 @@ -45,7 +57,9 @@ 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) { + return songs.filter(x => x.album) +} // #5 /** From e50abc080590c2f9cb2314083c482450c1c06646 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Wed, 10 Jan 2024 17:53:36 -0500 Subject: [PATCH 04/15] Solve problem #5 using .find() --- index.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index c90933f..2491d47 100644 --- a/index.js +++ b/index.js @@ -57,10 +57,16 @@ function categorizeSongsByRuntime(songs) { * @param {Object[]} songs - An array of songs. * @returns {string} The name of the album with the most songs. */ + function findAlbumWithMostSongs(songs) { - return songs.filter(x => x.album) + } +// Example usage +// const albumWithMostSongs = findAlbumWithMostSongs(songs); +// console.log(`Album with the most songs: ${albumWithMostSongs}`); + + // #5 /** * Returns details of the first song in a specific album. @@ -68,7 +74,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.find(song => song.album === albumName); +} // #6 /** @@ -166,21 +174,27 @@ 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) { + +} // Problem #18 /** * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { + +} // 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) { + +} // Problem #20 /** @@ -188,7 +202,9 @@ 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) { + +} module.exports = { From 5bf9c23bf1ba441e09fc2a04635f15a43c672393 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Wed, 10 Jan 2024 17:59:41 -0500 Subject: [PATCH 05/15] Solve problem #6 using .some() --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 2491d47..7597ebc 100644 --- a/index.js +++ b/index.js @@ -85,7 +85,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(x => x.runtimeInSeconds > runtime); +} // #7 /** From 7f3f90734e8fa48eedd5cb76d584c6c99e4f5325 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Thu, 11 Jan 2024 14:38:24 -0500 Subject: [PATCH 06/15] Solve problem #4 using .reduce() --- index.js | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 7597ebc..89657a8 100644 --- a/index.js +++ b/index.js @@ -59,7 +59,19 @@ function categorizeSongsByRuntime(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; } // Example usage @@ -95,7 +107,16 @@ 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(x => { + return { + title: x.title, + durationInMinutes: (x.runtimeInSeconds / 60) + } + }) +} + +// console.log(getSongsWithDurationInMinutes(exampleSongData)) // #8 /** @@ -103,7 +124,9 @@ 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) { + return songs.map(x => x.album).sort((a, b) => b.localeCompare(a)).filter((val, index) => songs.indexOf(val) === index); +} // #9 /** @@ -112,7 +135,9 @@ 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.filter(x => x.title.includes(word)); +} // #10 /** @@ -121,14 +146,18 @@ 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(x => x.artist === artistName).reduce((total, x) => total + x.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) { + +} // Problem #12 /** From 0360f0434c2ac82bd431bce65f19d9efe45e016d Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Thu, 11 Jan 2024 18:01:58 -0500 Subject: [PATCH 07/15] Solve problem #7 using .map() --- index.js | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 89657a8..9c7cc7c 100644 --- a/index.js +++ b/index.js @@ -156,7 +156,15 @@ function getTotalRuntimeOfArtist(songs, artistName) { * @param {Object[]} songs - An array of songs. */ function printArtistsWithMultipleSongs(songs) { - + // let artistWithTheMostSongs = {}; + // return songs.forEach(song => { + // if (artistWithTheMostSongs[song.artist]) { + // artistWithTheMostSongs[song.artist] + // } else { + // artistWithTheMostSongs[song.artist] += artistWithTheMostSongs[song.artist] + // } + // }) + // console.log(artistWithTheMostSongs); } // Problem #12 @@ -164,7 +172,12 @@ function printArtistsWithMultipleSongs(songs) { * Logs the longest song title. * @param {Object[]} songs - An array of songs. */ -function printLongestSongTitle(songs) {} +function printLongestSongTitle(songs) { + let longestSongTitle = songs.sort((a, b) => b.title.length - a.title.length)[0].title; + return longestSongTitle; +} + +console.log(printLongestSongTitle(exampleSongData)) // Problem #13 /** @@ -172,7 +185,9 @@ 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((a, b) => a.artist.localeCompare(b.artist) || a.title.localeCompare(b.title)); +} // Problem #14 /** @@ -180,7 +195,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) { +// return songs.map(x => { +// let album = songs.map(x => x.album); +// let total = songs.reduce((total, x) => total + x.runtimeInSeconds, 0) +// return { +// album: total +// } +// }) +// } + +function listAlbumTotalRuntimes(songs) { + // let albumTotalRuntimes = {}; + // songs.forEach(x => { + // if () + // }) + // return albumTotalRuntimes; +} + // Problem #15 /** @@ -189,7 +221,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(x => x.title.startsWith(letter)); +} // Problem #16 /** From e0bcb68e236337ab747197075260dc44d9a0ef6e Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Thu, 11 Jan 2024 20:11:10 -0500 Subject: [PATCH 08/15] Solve problem #9 using .map() and .filter() --- index.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 9c7cc7c..56cccaf 100644 --- a/index.js +++ b/index.js @@ -136,7 +136,7 @@ function getAlbumsInReverseOrder(songs) { * @returns {string[]} An array of song titles containing the word. */ function songsWithWord(songs, word) { - return songs.filter(x => x.title.includes(word)); + return songs.map(x => x.title).filter(x => x.includes(word)); } // #10 @@ -231,7 +231,9 @@ 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) { + +} // Problem #17 /** @@ -240,7 +242,8 @@ function mapArtistsToSongs(songs) {} * @returns {string} The name of the album with the longest average song runtime. */ function findAlbumWithLongestAverageRuntime(songs) { - + //need to find avg runtime of each album! + return songs.map(x => x.album).sort((a, b) => b.runtimeInSeconds - a.runtimeInSeconds)[0]; } // Problem #18 @@ -249,7 +252,7 @@ function findAlbumWithLongestAverageRuntime(songs) { * @param {Object[]} songs - An array of songs. */ function printSongsSortedByRuntime(songs) { - + console.log(songs.map(x => x.title).sort((a, b) => a.runtimeInSeconds - b.runtimeInSeconds)[0]); } // Problem #19 @@ -258,7 +261,15 @@ function printSongsSortedByRuntime(songs) { * @param {Object[]} songs - An array of songs. */ function printAlbumSummaries(songs) { + // return songs.forEach(x => { + // let totalRuntime = songs.forEach(song => ...?...); + // return { + // name: x.name, + // totalRuntime: totalRuntime, + // numberOfSongs : numberOfSongs + // } + // }) } // Problem #20 From a2055f4b86e0670ff84af09bee9a51cf62ac761e Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Fri, 12 Jan 2024 10:21:56 -0500 Subject: [PATCH 09/15] Solve problem #15 using .find() --- index.js | 55 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index 56cccaf..25470a2 100644 --- a/index.js +++ b/index.js @@ -125,7 +125,7 @@ function getSongsWithDurationInMinutes(songs) { * @returns {string[]} Array of album names in reverse alphabetical order. */ function getAlbumsInReverseOrder(songs) { - return songs.map(x => x.album).sort((a, b) => b.localeCompare(a)).filter((val, index) => songs.indexOf(val) === index); + return songs.map(x => x.album).filter((val, index) => songs.indexOf(val) === index).sort((a, b) => b.localeCompare(a)); } // #9 @@ -195,24 +195,18 @@ function sortSongsByArtistAndTitle(songs) { * @param {Object[]} songs - An array of songs. * @returns {Object} An object mapping each album to its total runtime. */ -// function listAlbumTotalRuntimes(songs) { -// return songs.map(x => { -// let album = songs.map(x => x.album); -// let total = songs.reduce((total, x) => total + x.runtimeInSeconds, 0) -// return { -// album: total -// } -// }) -// } - function listAlbumTotalRuntimes(songs) { - // let albumTotalRuntimes = {}; - // songs.forEach(x => { - // if () - // }) - // return albumTotalRuntimes; + } +// function listAlbumTotalRuntimes(songs) { +// // let albumTotalRuntimes = {}; +// // songs.forEach(x => { +// // if () +// // }) +// // return albumTotalRuntimes; +// } + // Problem #15 /** @@ -221,8 +215,12 @@ 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) { +// return songs.find(x => x.title.startsWith(letter)); +// } + function findFirstSongStartingWith(songs, letter) { - return songs.find(x => x.title.startsWith(letter)); + return songs.find(x => x.title[0].startsWith(letter)); } // Problem #16 @@ -232,9 +230,30 @@ function findFirstSongStartingWith(songs, letter) { * @returns {Object} An object mapping each artist to an array of their song titles. */ function mapArtistsToSongs(songs) { - + return songs.forEach(x => { + let artist = x.artist; + let songTitles = songs.map(x => x.title) + return { + artist: x.artist, + songs: songTitles + } + }) } + + + +// function mapArtistsToSongs(songs) { +// return songs.forEach(x => { +// let artist = x.artist; +// let songTitles = songs.map(x => x.title) +// return { +// artist: x.artist, +// songs: songTitles +// } +// }) +// } + // Problem #17 /** * Finds the album with the longest average song runtime. From 5ef1fc737f5f86a26ca7c8c476a9f805b8e0139d Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Sat, 13 Jan 2024 11:08:27 -0500 Subject: [PATCH 10/15] Solve problem #16 using .map() --- index.js | 84 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/index.js b/index.js index 25470a2..8e9a79d 100644 --- a/index.js +++ b/index.js @@ -127,7 +127,6 @@ function getSongsWithDurationInMinutes(songs) { function getAlbumsInReverseOrder(songs) { return songs.map(x => x.album).filter((val, index) => songs.indexOf(val) === index).sort((a, b) => b.localeCompare(a)); } - // #9 /** * Returns a list of song titles that contain a specific word. @@ -215,12 +214,8 @@ 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) { -// return songs.find(x => x.title.startsWith(letter)); -// } - function findFirstSongStartingWith(songs, letter) { - return songs.find(x => x.title[0].startsWith(letter)); + return songs.find(x => x.title[0].startsWith(letter)) || null; } // Problem #16 @@ -229,31 +224,21 @@ 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. */ +// SOLUTION function mapArtistsToSongs(songs) { - return songs.forEach(x => { - let artist = x.artist; - let songTitles = songs.map(x => x.title) - return { - artist: x.artist, - songs: songTitles + let artistMap = {}; + + songs.map(song => { + if(!artistMap.hasOwnProperty(song.artist)){ + artistMap[song.artist] = []; + } + else { + artistMap[song.artist].push(song.title); } }) + return artistMap; } - - - -// function mapArtistsToSongs(songs) { -// return songs.forEach(x => { -// let artist = x.artist; -// let songTitles = songs.map(x => x.title) -// return { -// artist: x.artist, -// songs: songTitles -// } -// }) -// } - // Problem #17 /** * Finds the album with the longest average song runtime. @@ -270,8 +255,12 @@ function findAlbumWithLongestAverageRuntime(songs) { * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ +// function printSongsSortedByRuntime(songs) { +// console.log(songs.map(x => x.title).sort((a, b) => a.runtimeInSeconds - b.runtimeInSeconds)[0]); +// } function printSongsSortedByRuntime(songs) { - console.log(songs.map(x => x.title).sort((a, b) => a.runtimeInSeconds - b.runtimeInSeconds)[0]); + let songsSortedByRuntime = songs.sort((songA, songB) => songA.runtimeInSeconds - songB.runtimeInSeconds); + songsSortedByRuntime.forEach(song => console.log(song.title)) } // Problem #19 @@ -280,17 +269,40 @@ function printSongsSortedByRuntime(songs) { * @param {Object[]} songs - An array of songs. */ function printAlbumSummaries(songs) { - // return songs.forEach(x => { - // let totalRuntime = songs.forEach(song => ...?...); - - // return { - // name: x.name, - // totalRuntime: totalRuntime, - // numberOfSongs : numberOfSongs - // } - // }) + let albumSummaries = {}; + songs.forEach(song => { + if (!albumSummaries[song.album]) { + albumSummaries[song.album] = { + albumName: song.album, + songCount: 1, + totalRuntime: song.runtimeInSeconds + }; + } else { + albumSummaries[song.album].songCount++ + albumSummaries[song.album].totalRuntime += song.runtimeInSeconds; + } + }); + for (const summary of albumSummaries) { + console.log(summary); + } } + + + + +// function printAlbumSummaries(songs) { +// // return songs.forEach(x => { +// // let totalRuntime = songs.forEach(song => ...?...); + +// // return { +// // name: x.name, +// // totalRuntime: totalRuntime, +// // numberOfSongs : numberOfSongs +// // } +// // }) +// } + // Problem #20 /** * Finds the artist with the most songs in the list. From 7af0fdb233c1ea48642aefc709edbc6613e95574 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Sun, 14 Jan 2024 10:41:23 -0500 Subject: [PATCH 11/15] Solve problem #19 using .forEach and for-in loop --- index.js | 42 +++++++++--------------------------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 8e9a79d..70154dc 100644 --- a/index.js +++ b/index.js @@ -198,14 +198,6 @@ function listAlbumTotalRuntimes(songs) { } -// function listAlbumTotalRuntimes(songs) { -// // let albumTotalRuntimes = {}; -// // songs.forEach(x => { -// // if () -// // }) -// // return albumTotalRuntimes; -// } - // Problem #15 /** @@ -224,13 +216,12 @@ 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. */ -// SOLUTION function mapArtistsToSongs(songs) { let artistMap = {}; songs.map(song => { if(!artistMap.hasOwnProperty(song.artist)){ - artistMap[song.artist] = []; + artistMap[song.artist] = [song.title]; } else { artistMap[song.artist].push(song.title); @@ -264,45 +255,30 @@ function printSongsSortedByRuntime(songs) { } // 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 => { + songs.forEach((song) => { if (!albumSummaries[song.album]) { albumSummaries[song.album] = { - albumName: song.album, songCount: 1, - totalRuntime: song.runtimeInSeconds + totalRuntime: song.runtimeInSeconds, }; } else { - albumSummaries[song.album].songCount++ + albumSummaries[song.album].songCount++; albumSummaries[song.album].totalRuntime += song.runtimeInSeconds; } }); - for (const summary of albumSummaries) { - console.log(summary); + for (const summary in albumSummaries) { + console.log( + `${summary}: ${albumSummaries[summary].songCount} songs, Total Runtime: ${albumSummaries[summary].totalRuntime} seconds` + ); } } - - - - -// function printAlbumSummaries(songs) { -// // return songs.forEach(x => { -// // let totalRuntime = songs.forEach(song => ...?...); - -// // return { -// // name: x.name, -// // totalRuntime: totalRuntime, -// // numberOfSongs : numberOfSongs -// // } -// // }) -// } - // Problem #20 /** * Finds the artist with the most songs in the list. From f30acde7bf7bbd2aec0b77ecca59238b8a4aa8c1 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Mon, 15 Jan 2024 10:08:22 -0500 Subject: [PATCH 12/15] Solve problem #14 using .forEach() --- index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 70154dc..1dfcd9b 100644 --- a/index.js +++ b/index.js @@ -17,7 +17,7 @@ function getSortedTitles(songs) { return songs.map(x => x.title).sort() } -console.log(getSortedTitles(exampleSongData)) +// console.log(getSortedTitles(exampleSongData)) // #2 /** @@ -194,8 +194,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) { + let albumTotalRuntime = {}; + songs.forEach(song => { + if (!albumTotalRuntime[song.album]) { + albumTotalRuntime[song.album] = song.runtimeInSeconds; + } else { + albumTotalRuntime[song.album] += song.runtimeInSeconds + } + }) + return albumTotalRuntime; } @@ -237,8 +246,7 @@ function mapArtistsToSongs(songs) { * @returns {string} The name of the album with the longest average song runtime. */ function findAlbumWithLongestAverageRuntime(songs) { - //need to find avg runtime of each album! - return songs.map(x => x.album).sort((a, b) => b.runtimeInSeconds - a.runtimeInSeconds)[0]; + } // Problem #18 From 920bf0a0aaa5b2e61299a7461d9e35cdaee46195 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Mon, 15 Jan 2024 10:18:09 -0500 Subject: [PATCH 13/15] Solve problem #17 using .forEach() --- index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/index.js b/index.js index 1dfcd9b..edafc20 100644 --- a/index.js +++ b/index.js @@ -246,7 +246,28 @@ function mapArtistsToSongs(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 = Number(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; } // Problem #18 From c450486e1bcbc4fb56ac2ab3f8eff7de526ce9b6 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Mon, 15 Jan 2024 10:48:28 -0500 Subject: [PATCH 14/15] Solve problem #12 using .sort() --- index.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index edafc20..34ae435 100644 --- a/index.js +++ b/index.js @@ -172,11 +172,9 @@ function printArtistsWithMultipleSongs(songs) { * @param {Object[]} songs - An array of songs. */ function printLongestSongTitle(songs) { - let longestSongTitle = songs.sort((a, b) => b.title.length - a.title.length)[0].title; - return longestSongTitle; + console.log(songs.sort((a, b) => b.title.length - a.title.length)[0].title); } -console.log(printLongestSongTitle(exampleSongData)) // Problem #13 /** @@ -185,7 +183,7 @@ console.log(printLongestSongTitle(exampleSongData)) * @returns {Object[]} Sorted array of songs. */ function sortSongsByArtistAndTitle(songs) { - return songs.sort((a, b) => a.artist.localeCompare(b.artist) || a.title.localeCompare(b.title)); + return songs.sort((a, b) => a.artist.localeCompare(b.artist).sort((a,b) => a.title.localeCompare(b.title))); } // Problem #14 @@ -315,7 +313,23 @@ function printAlbumSummaries(songs) { * @returns {string} The name of the artist with the most songs. */ function findArtistWithMostSongs(songs) { + let artistWithTheMostSongs = {}; + let songCount = 1; + let highestSongCount = 0; + let artistName = ""; + songs.forEach(song => { + if (!artistWithTheMostSongs[song.artist]) { + artistWithTheMostSongs[song.artist] = songCount; + } else { + artistWithTheMostSongs[song.artist]++; + } + if (artistWithTheMostSongs[song.artist].songCount > highestSongCount) { + highestSongCount = artistWithTheMostSongs[song.artist].songCount; + } + artistName = song.artist; + }) + return artistName } From 09eced0ab3e382f5d83a47c7a326137a083804a4 Mon Sep 17 00:00:00 2001 From: alexandra-perez Date: Tue, 16 Jan 2024 12:47:49 -0500 Subject: [PATCH 15/15] Initial commit --- index.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index 34ae435..b4a32ec 100644 --- a/index.js +++ b/index.js @@ -155,15 +155,18 @@ function getTotalRuntimeOfArtist(songs, artistName) { * @param {Object[]} songs - An array of songs. */ function printArtistsWithMultipleSongs(songs) { - // let artistWithTheMostSongs = {}; - // return songs.forEach(song => { - // if (artistWithTheMostSongs[song.artist]) { - // artistWithTheMostSongs[song.artist] - // } else { - // artistWithTheMostSongs[song.artist] += artistWithTheMostSongs[song.artist] - // } - // }) - // console.log(artistWithTheMostSongs); + let artistsWithMultipleSongs = {}; + let songCount = 1; + + songs.forEach(song => { + if (!artistsWithMultipleSongs[song.artist]) { + artistsWithMultipleSongs[song.artist] = songCount; + } else { + artistsWithMultipleSongs[song.artist]++; + } + return artistsWithMultipleSongs[song.artist] + }) + console.log(printArtistsWithMultipleSongs(exampleSongData)); } // Problem #12