From eb69b064394cc1e7172cdba6878961e5a85ac643 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 13:34:15 -0500 Subject: [PATCH 01/33] Completed Problem 1 using map --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..13d5363 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,9 @@ 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(); +} // #2 /** @@ -22,7 +24,13 @@ 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 => { + return x.album === albumName ? x.title : + }) +} + +console.log(getSongsFromAlbum(exampleSongData)) // #3 /** From 6b201d995a52b2b6d286e2ebe1adf3ea5ea65544 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 13:50:04 -0500 Subject: [PATCH 02/33] Completed Question 2 --- index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 13d5363..2f80c8c 100644 --- a/index.js +++ b/index.js @@ -25,12 +25,16 @@ function getSortedTitles(songs) { * @returns {string[]} An array of song titles. */ function getSongsFromAlbum(songs, albumName) { - return songs.filter(x => { - return x.album === albumName ? x.title : - }) + let newArr = []; + for(let i = 0; i < songs.length; i++){ + if(songs[i].album === albumName){ + newArr.push(songs[i].title) + } + } + return newArr } -console.log(getSongsFromAlbum(exampleSongData)) +console.log(getSongsFromAlbum(exampleSongData, "Bluewerks Vol. 1: Up Down Left Right")) // #3 /** From 928aed1dc308905e16647e25a876c58ddf87a218 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 14:06:16 -0500 Subject: [PATCH 03/33] completed question 3 --- index.js | 27 +++++++++++++++++++++++++-- practiceLane.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 practiceLane.js diff --git a/index.js b/index.js index 2f80c8c..ea057d4 100644 --- a/index.js +++ b/index.js @@ -34,7 +34,6 @@ function getSongsFromAlbum(songs, albumName) { return newArr } -console.log(getSongsFromAlbum(exampleSongData, "Bluewerks Vol. 1: Up Down Left Right")) // #3 /** @@ -42,7 +41,31 @@ console.log(getSongsFromAlbum(exampleSongData, "Bluewerks Vol. 1: Up Down Left R * @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) { + let shortSongs = songs.reduce((total, song) => { + if(song.runtimeInSeconds < 180) { + total++ + } + return total + }, 0) + let mediumSongs = songs.reduce((total, song) => { + if(song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300) { + total++ + } + return total + }, 0) + let longSongs = songs.reduce((total, song) => { + if(song.runtimeInSeconds > 300) { + total++ + } + return total + }, 0) + return { + shortSongs: shortSongs, + mediumSongs: mediumSongs, + longSongs: longSongs + } +} // #4 /** diff --git a/practiceLane.js b/practiceLane.js new file mode 100644 index 0000000..54311b7 --- /dev/null +++ b/practiceLane.js @@ -0,0 +1,29 @@ +const exampleSongData = require("./data/songs"); + +function categorizeSongsByRuntime(songs) { + let shortSongs = songs.reduce((total, song) => { + if(song.runtimeInSeconds < 180) { + total++ + } + return total + }, 0) + let mediumSongs = songs.reduce((total, song) => { + if(song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300) { + total++ + } + return total + }, 0) + let longSongs = songs.reduce((total, song) => { + if(song.runtimeInSeconds > 300) { + total++ + } + return total + }, 0) + return { + shortSongs: shortSongs, + mediumSongs: mediumSongs, + longSongs: longSongs + } + } + + console.log(categorizeSongsByRuntime(exampleSongData)) \ No newline at end of file From b13ffd462601a19763c4951da37ad4627d1ef863 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 15:54:46 -0500 Subject: [PATCH 04/33] completed question 4 --- .vscode/launch.json | 20 ++++++++++++++++++ index.js | 31 ++++++++++++++++++++++++++-- practiceLane.js | 49 +++++++++++++++++++++++---------------------- 3 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..b426168 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + + + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/index.js" + } + ] +} \ No newline at end of file diff --git a/index.js b/index.js index ea057d4..3001396 100644 --- a/index.js +++ b/index.js @@ -73,7 +73,32 @@ 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 albums = songs.map(song => song.album) + let obj = {} + let mostSongsValue = 0 + let album; + for(let i = 0; i < songs.length; i++) { + if(obj.hasOwnProperty(songs[i].album)){ + continue; + } + obj[songs[i].album] = 0 + } + for(let j = 0; j < albums.length; j++) { + for(let key in obj) { + if(albums[j] === key){ + obj[key]++ + } + } + } + for(let key in obj) { + if(obj[key] > mostSongsValue) { + mostSongsValue = obj[key] + album = key + } + } + return album +} // #5 /** @@ -82,7 +107,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) { + +} // #6 /** diff --git a/practiceLane.js b/practiceLane.js index 54311b7..00cb60f 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,29 +1,30 @@ const exampleSongData = require("./data/songs"); -function categorizeSongsByRuntime(songs) { - let shortSongs = songs.reduce((total, song) => { - if(song.runtimeInSeconds < 180) { - total++ - } - return total - }, 0) - let mediumSongs = songs.reduce((total, song) => { - if(song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300) { - total++ - } - return total - }, 0) - let longSongs = songs.reduce((total, song) => { - if(song.runtimeInSeconds > 300) { - total++ - } - return total - }, 0) - return { - shortSongs: shortSongs, - mediumSongs: mediumSongs, - longSongs: longSongs +function findAlbumWithMostSongs(songs) { + let albums = songs.map(song => song.album) + let obj = {} + let mostSongsValue = 0 + let album; + for(let i = 0; i < songs.length; i++) { + if(obj.hasOwnProperty(songs[i].album)){ + continue; + } + obj[songs[i].album] = 0 } + for(let j = 0; j < albums.length; j++) { + for(let key in obj) { + if(albums[j] === key){ + obj[key]++ + } + } + } + for(let key in obj) { + if(obj[key] > mostSongsValue) { + mostSongsValue = obj[key] + album = key + } + } + return album } - console.log(categorizeSongsByRuntime(exampleSongData)) \ No newline at end of file + console.log(findAlbumWithMostSongs(exampleSongData)) \ No newline at end of file From c1371af30f570b841887fafbe750b7a9ed107480 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 16:05:14 -0500 Subject: [PATCH 05/33] completed question 5 --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 3001396..042f282 100644 --- a/index.js +++ b/index.js @@ -108,7 +108,7 @@ function findAlbumWithMostSongs(songs) { * @returns {Object|null} First song object in the album or null. */ function getFirstSongInAlbum(songs, albumName) { - + return songs.find(song => song.album === albumName) } // #6 @@ -118,7 +118,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) { + +} // #7 /** From 98ff1ab6bc63839af40621384dfc4ee9dd26588c Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 16:11:36 -0500 Subject: [PATCH 06/33] completed question 5 --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 042f282..ff72e73 100644 --- a/index.js +++ b/index.js @@ -119,7 +119,7 @@ function getFirstSongInAlbum(songs, albumName) { * @returns {boolean} True if there is at least one song longer than the runtime. */ function isThereLongSong(songs, runtime) { - + return songs.some(song => song.runtimeInSeconds > runtime) } // #7 @@ -128,7 +128,9 @@ 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) { + +} // #8 /** From e306e502b7afba5db84d3bf5339f517f766c7332 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 18:05:38 -0500 Subject: [PATCH 07/33] completed 3 questions just now --- index.js | 18 ++++++++++++++++-- practiceLane.js | 35 +++++++++-------------------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index ff72e73..b48c354 100644 --- a/index.js +++ b/index.js @@ -129,7 +129,13 @@ function isThereLongSong(songs, runtime) { * @returns {Object[]} Array of song objects with runtime in minutes. */ function getSongsWithDurationInMinutes(songs) { - + let array = [] + for(let i = 0; i < songs.length; i++) { + let obj = {}; + obj[songs[i].title] = Math.round(songs[i].runtimeInSeconds / 60) + array.push(obj); + } + return array; } // #8 @@ -138,7 +144,15 @@ 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(song => song.album).sort((a, b) => { + if(a > b) { + return 1 + }else { + return -1 + } + }) +} // #9 /** diff --git a/practiceLane.js b/practiceLane.js index 00cb60f..63653d0 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,30 +1,13 @@ const exampleSongData = require("./data/songs"); -function findAlbumWithMostSongs(songs) { - let albums = songs.map(song => song.album) - let obj = {} - let mostSongsValue = 0 - let album; - for(let i = 0; i < songs.length; i++) { - if(obj.hasOwnProperty(songs[i].album)){ - continue; - } - obj[songs[i].album] = 0 - } - for(let j = 0; j < albums.length; j++) { - for(let key in obj) { - if(albums[j] === key){ - obj[key]++ - } - } - } - for(let key in obj) { - if(obj[key] > mostSongsValue) { - mostSongsValue = obj[key] - album = key - } - } - return album +function getAlbumsInReverseOrder(songs) { + return songs.map(song => song.album).sort((a, b) => { + if(a > b) { + return -1 + }else { + return 1 + } + } ) } - console.log(findAlbumWithMostSongs(exampleSongData)) \ No newline at end of file +console.log(getAlbumsInReverseOrder(exampleSongData)) From 25fc37e79a85f1423b1763255149cad0582d2b61 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 18:14:32 -0500 Subject: [PATCH 08/33] completed question 9 --- index.js | 4 +++- practiceLane.js | 12 +++--------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index b48c354..06dcc99 100644 --- a/index.js +++ b/index.js @@ -161,7 +161,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(song => song[title].includes(word)) +} // #10 /** diff --git a/practiceLane.js b/practiceLane.js index 63653d0..b3f6502 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,13 +1,7 @@ const exampleSongData = require("./data/songs"); -function getAlbumsInReverseOrder(songs) { - return songs.map(song => song.album).sort((a, b) => { - if(a > b) { - return -1 - }else { - return 1 - } - } ) +function songsWithWord(songs, word) { + return songs.filter(song => song.title.includes(word)) } -console.log(getAlbumsInReverseOrder(exampleSongData)) +console.log(songsWithWord(exampleSongData, "Berlin")) From cf733a9a3dea470821dbd3589731b64753483710 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 19:00:06 -0500 Subject: [PATCH 09/33] completed question 12 --- data/songs.js | 6 ++++++ index.js | 38 ++++++++++++++++++++++++++++++++++---- practiceLane.js | 27 ++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/data/songs.js b/data/songs.js index 6fd56d6..e6cecfc 100644 --- a/data/songs.js +++ b/data/songs.js @@ -137,6 +137,12 @@ const songs = [ album: "Seasonal Sounds", artist: "Melody Green", runtimeInSeconds: 152, + }, + { + title: "Nightfall Serenade Berline", + album: "Seasonal Sounds", + artist: "Melody Green", + runtimeInSeconds: 152, } ]; diff --git a/index.js b/index.js index 06dcc99..b9fc8e9 100644 --- a/index.js +++ b/index.js @@ -162,7 +162,7 @@ function getAlbumsInReverseOrder(songs) { * @returns {string[]} An array of song titles containing the word. */ function songsWithWord(songs, word) { - return songs.filter(song => song[title].includes(word)) + return songs.filter(song => song.title.includes(word)) } // #10 @@ -172,21 +172,51 @@ 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.reduce((total, song) => { + if(song.artist == artistName){ + total += song.runtimeInSeconds + } + return total + },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) { + let artist = songs.map(song => song.artist) + let obj = {} + for(let i = 0; i < songs.length; i++) { + if(obj.hasOwnProperty(songs[i].artist)){ + continue; + } + obj[songs[i].artist] = 0 + } + for(let j = 0; j < artist.length; j++) { + for(let key in obj) { + if(artist[j] === key){ + obj[key]++ + } + } + } + for(let key in obj) { + if(obj[key] > 1 ) { + console.log(key) + } + } +} // Problem #12 /** * Logs the longest song title. * @param {Object[]} songs - An array of songs. */ -function printLongestSongTitle(songs) {} +function printLongestSongTitle(songs) { + +} // Problem #13 /** diff --git a/practiceLane.js b/practiceLane.js index b3f6502..f10177c 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,7 +1,28 @@ const exampleSongData = require("./data/songs"); -function songsWithWord(songs, word) { - return songs.filter(song => song.title.includes(word)) +function printArtistsWithMultipleSongs(songs) { + let artist = songs.map(song => song.artist) + let obj = {} + let mostSongsValue = 0 + let album; + for(let i = 0; i < songs.length; i++) { + if(obj.hasOwnProperty(songs[i].artist)){ + continue; + } + obj[songs[i].artist] = 0 + } + for(let j = 0; j < artist.length; j++) { + for(let key in obj) { + if(artist[j] === key){ + obj[key]++ + } + } + } + for(let key in obj) { + if(obj[key] > 1 ) { + console.log(key) + } + } } -console.log(songsWithWord(exampleSongData, "Berlin")) +console.log(printArtistsWithMultipleSongs(exampleSongData, "Saib")) From 4a148950f05e4cddb03276f7f2a0d45190a57b13 Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 22:20:37 -0500 Subject: [PATCH 10/33] completed question sorry i forgot to do this a couple times --- data/songs.js | 6 ------ index.js | 40 +++++++++++++++++++++++++++++----------- practiceLane.js | 35 +++++++++++------------------------ 3 files changed, 40 insertions(+), 41 deletions(-) diff --git a/data/songs.js b/data/songs.js index e6cecfc..0ad05d8 100644 --- a/data/songs.js +++ b/data/songs.js @@ -138,12 +138,6 @@ const songs = [ artist: "Melody Green", runtimeInSeconds: 152, }, - { - title: "Nightfall Serenade Berline", - album: "Seasonal Sounds", - artist: "Melody Green", - runtimeInSeconds: 152, - } ]; module.exports = songs; diff --git a/index.js b/index.js index b9fc8e9..cae98e6 100644 --- a/index.js +++ b/index.js @@ -129,13 +129,10 @@ function isThereLongSong(songs, runtime) { * @returns {Object[]} Array of song objects with runtime in minutes. */ function getSongsWithDurationInMinutes(songs) { - let array = [] for(let i = 0; i < songs.length; i++) { - let obj = {}; - obj[songs[i].title] = Math.round(songs[i].runtimeInSeconds / 60) - array.push(obj); + songs[i].durationInMinutes = songs[i].runtimeInSeconds / 60 } - return array; + return songs; } // #8 @@ -145,13 +142,19 @@ function getSongsWithDurationInMinutes(songs) { * @returns {string[]} Array of album names in reverse alphabetical order. */ function getAlbumsInReverseOrder(songs) { - return songs.map(song => song.album).sort((a, b) => { + let array = songs.map(song => song.album).sort((a, b) => { if(a > b) { - return 1 - }else { return -1 + }else { + return 1 } }) + for(let i = 0; i < array.length; i++) { + if(array[i] === array[i + 1] || array[i] === array[i - 1]){ + array.splice(i,1) + } + } + return array } // #9 @@ -162,9 +165,15 @@ function getAlbumsInReverseOrder(songs) { * @returns {string[]} An array of song titles containing the word. */ function songsWithWord(songs, word) { - return songs.filter(song => song.title.includes(word)) + let array = [] + let newArr = songs.filter(song => song.title.includes(word)) + for(const obj of newArr){ + array.push(obj.title) + } + return array } + // #10 /** * Returns the total runtime of songs by a specific artist. @@ -215,7 +224,13 @@ function printArtistsWithMultipleSongs(songs) { * @param {Object[]} songs - An array of songs. */ function printLongestSongTitle(songs) { - + let longestSong = songs.reduce((longest, song) => { + if(longest.length < song.title.length) { + longest = song.title + } + return longest + },"") + console.log(longestSong) } // Problem #13 @@ -224,7 +239,10 @@ function printLongestSongTitle(songs) { * @param {Object[]} songs - An array of songs. * @returns {Object[]} Sorted array of songs. */ -function sortSongsByArtistAndTitle(songs) {} +function sortSongsByArtistAndTitle(songs) { + let sortArtist = songs.sort((a, b) => a.artist.localeCompare(b.artist)) + return sortArtist.sort((a, b) => a.title.localeCompare(b.title)) +} // Problem #14 /** diff --git a/practiceLane.js b/practiceLane.js index f10177c..adbee40 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,28 +1,15 @@ +const { get } = require("http"); const exampleSongData = require("./data/songs"); -function printArtistsWithMultipleSongs(songs) { - let artist = songs.map(song => song.artist) - let obj = {} - let mostSongsValue = 0 - let album; - for(let i = 0; i < songs.length; i++) { - if(obj.hasOwnProperty(songs[i].artist)){ - continue; - } - obj[songs[i].artist] = 0 - } - for(let j = 0; j < artist.length; j++) { - for(let key in obj) { - if(artist[j] === key){ - obj[key]++ - } - } - } - for(let key in obj) { - if(obj[key] > 1 ) { - console.log(key) - } - } +function printLongestSongTitle(songs) { + let longestSong = songs.reduce((longest, song) => { + if(longest.length < song.title.length) { + longest = song.title + } + return longest + },"") + return longestSong } -console.log(printArtistsWithMultipleSongs(exampleSongData, "Saib")) +console.log(printLongestSongTitle(exampleSongData)) + From d1adc42b49f413f2d96d8933f404894a09c20ccf Mon Sep 17 00:00:00 2001 From: llamouth Date: Mon, 8 Jan 2024 22:23:31 -0500 Subject: [PATCH 11/33] completed question 13 --- index.js | 10 +++++++--- practiceLane.js | 13 ++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index cae98e6..7f371e4 100644 --- a/index.js +++ b/index.js @@ -240,8 +240,10 @@ function printLongestSongTitle(songs) { * @returns {Object[]} Sorted array of songs. */ function sortSongsByArtistAndTitle(songs) { - let sortArtist = songs.sort((a, b) => a.artist.localeCompare(b.artist)) - return sortArtist.sort((a, b) => a.title.localeCompare(b.title)) + const sorted = songs.slice().sort((a, b) => { + return a.artist.localeCompare(b.artist) || a.title.localeCompare(b.title); + }) + return sorted } // Problem #14 @@ -250,7 +252,9 @@ 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) { + +} // Problem #15 /** diff --git a/practiceLane.js b/practiceLane.js index adbee40..ed3f927 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,15 +1,10 @@ const { get } = require("http"); const exampleSongData = require("./data/songs"); -function printLongestSongTitle(songs) { - let longestSong = songs.reduce((longest, song) => { - if(longest.length < song.title.length) { - longest = song.title - } - return longest - },"") - return longestSong +function sortSongsByArtistAndTitle(songs) { + let sortArtist = songs.sort((a, b) => a.artist.localeCompare(b.artist)) + return sortArtist.sort((a, b) => a.title.localeCompare(b.title)) } -console.log(printLongestSongTitle(exampleSongData)) +console.log(sortSongsByArtistAndTitle(exampleSongData)) From c18eb0416f025e97da38255aceb95ef27b239026 Mon Sep 17 00:00:00 2001 From: llamouth Date: Tue, 9 Jan 2024 08:39:04 -0500 Subject: [PATCH 12/33] back to work --- index.js | 62 +++++++++++++++++++++++++++++-------------------- practiceLane.js | 18 ++++++++------ 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 7f371e4..2a1eac5 100644 --- a/index.js +++ b/index.js @@ -79,17 +79,17 @@ function findAlbumWithMostSongs(songs) { let mostSongsValue = 0 let album; for(let i = 0; i < songs.length; i++) { - if(obj.hasOwnProperty(songs[i].album)){ - continue; - } - obj[songs[i].album] = 0 + if(obj.hasOwnProperty(songs[i].album)){ + continue; + } + obj[songs[i].album] = 0 } for(let j = 0; j < albums.length; j++) { - for(let key in obj) { - if(albums[j] === key){ - obj[key]++ - } + for(let key in obj) { + if(albums[j] === key){ + obj[key]++ } + } } for(let key in obj) { if(obj[key] > mostSongsValue) { @@ -168,7 +168,7 @@ function songsWithWord(songs, word) { let array = [] let newArr = songs.filter(song => song.title.includes(word)) for(const obj of newArr){ - array.push(obj.title) + array.push(obj.title) } return array } @@ -199,22 +199,22 @@ function printArtistsWithMultipleSongs(songs) { let artist = songs.map(song => song.artist) let obj = {} for(let i = 0; i < songs.length; i++) { - if(obj.hasOwnProperty(songs[i].artist)){ - continue; - } - obj[songs[i].artist] = 0 + if(obj.hasOwnProperty(songs[i].artist)){ + continue; + } + obj[songs[i].artist] = 0 } for(let j = 0; j < artist.length; j++) { - for(let key in obj) { - if(artist[j] === key){ - obj[key]++ - } + for(let key in obj) { + if(artist[j] === key){ + obj[key]++ } + } } for(let key in obj) { - if(obj[key] > 1 ) { - console.log(key) - } + if(obj[key] > 1 ) { + console.log(key) + } } } @@ -240,12 +240,13 @@ function printLongestSongTitle(songs) { * @returns {Object[]} Sorted array of songs. */ function sortSongsByArtistAndTitle(songs) { - const sorted = songs.slice().sort((a, b) => { + return songs.sort((a, b) => { return a.artist.localeCompare(b.artist) || a.title.localeCompare(b.title); }) - return sorted } + + // Problem #14 /** * Lists albums along with their total runtime. @@ -253,7 +254,14 @@ function sortSongsByArtistAndTitle(songs) { * @returns {Object} An object mapping each album to its total runtime. */ function listAlbumTotalRuntimes(songs) { - + return songs.reduce((obj, song) => { + if(obj.hasOwnProperty(song.album)) { + obj[song.album] += song.runtimeInSeconds + }else { + obj[song.album] = song.runtimeInSeconds + } + return obj; + },{}) } // Problem #15 @@ -263,7 +271,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.charAt(0) === letter ? song : null) +} // Problem #16 /** @@ -271,7 +281,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 /** diff --git a/practiceLane.js b/practiceLane.js index ed3f927..3adeed6 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,10 +1,14 @@ const { get } = require("http"); const exampleSongData = require("./data/songs"); -function sortSongsByArtistAndTitle(songs) { - let sortArtist = songs.sort((a, b) => a.artist.localeCompare(b.artist)) - return sortArtist.sort((a, b) => a.title.localeCompare(b.title)) - } - -console.log(sortSongsByArtistAndTitle(exampleSongData)) - +function listAlbumTotalRuntimes(songs) { + const albumObj = {} + for(let i = 0; i < songs.length; i++) { + if(albumObj.hasOwnProperty(songs[i].album)){ + albumObj[songs[i].album] += songs[i].runtimeInSeconds + } + albumObj[songs[i].album] = songs[i].runtimeInSeconds + } + return albumObj +} +// console.log(listAlbumTotalRuntimes(exampleSongData)) From 54b1ae8896bf6d978201a6198cdc7791c88bdcc8 Mon Sep 17 00:00:00 2001 From: llamouth Date: Tue, 9 Jan 2024 09:11:03 -0500 Subject: [PATCH 13/33] completed question 16 --- index.js | 18 +++++++++++++++++- practiceLane.js | 25 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 2a1eac5..18a0106 100644 --- a/index.js +++ b/index.js @@ -281,10 +281,26 @@ 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 artists = songs.map(song => song.artist) + const mappedObject = {}; + for(const artist of artists) { + const arr = [] + for(const song of songs) { + if(artist === song.artist) { + arr.push(song.title) + } + } + if(mappedObject.hasOwnProperty(artist)){ + continue; + } + mappedObject[artist] = arr + } + return mappedObject } + // Problem #17 /** * Finds the album with the longest average song runtime. diff --git a/practiceLane.js b/practiceLane.js index 3adeed6..83e033e 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,14 +1,21 @@ const { get } = require("http"); const exampleSongData = require("./data/songs"); -function listAlbumTotalRuntimes(songs) { - const albumObj = {} - for(let i = 0; i < songs.length; i++) { - if(albumObj.hasOwnProperty(songs[i].album)){ - albumObj[songs[i].album] += songs[i].runtimeInSeconds +function mapArtistsToSongs(songs) { + const artists = songs.map(song => song.artist) + const mappedObject = {}; + for(const artist of artists) { + const arr = [] + for(const song of songs) { + if(artist === song.artist) { + arr.push(song.title) + } + } + if(mappedObject.hasOwnProperty(artist)){ + continue; + } + mappedObject[artist] = arr } - albumObj[songs[i].album] = songs[i].runtimeInSeconds - } - return albumObj + return mappedObject } -// console.log(listAlbumTotalRuntimes(exampleSongData)) +console.log(mapArtistsToSongs(exampleSongData)) From 3cdebee21f01b692b804c0fd6650e8b409feeb14 Mon Sep 17 00:00:00 2001 From: llamouth Date: Tue, 9 Jan 2024 10:42:10 -0500 Subject: [PATCH 14/33] completed test --- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 18a0106..1685894 100644 --- a/index.js +++ b/index.js @@ -74,16 +74,19 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { + // ! I wanted to create an object with the key being the album name and the values being the amount of songs on the album let albums = songs.map(song => song.album) let obj = {} let mostSongsValue = 0 let album; + // ! This loop is creating key(album) value(0) pairs in the obj variable. if the key is already created it continues in the loop so there is no duplicates for(let i = 0; i < songs.length; i++) { if(obj.hasOwnProperty(songs[i].album)){ continue; } obj[songs[i].album] = 0 } + // ! These loops is counting the songs with the same album name to give values in the obj variable for(let j = 0; j < albums.length; j++) { for(let key in obj) { if(albums[j] === key){ @@ -91,11 +94,12 @@ function findAlbumWithMostSongs(songs) { } } } + // ! this loop is assigning the album with the most songs to return the album with the most songs for(let key in obj) { - if(obj[key] > mostSongsValue) { - mostSongsValue = obj[key] - album = key - } + if(obj[key] > mostSongsValue) { + mostSongsValue = obj[key] + album = key + } } return album } @@ -142,6 +146,7 @@ function getSongsWithDurationInMinutes(songs) { * @returns {string[]} Array of album names in reverse alphabetical order. */ function getAlbumsInReverseOrder(songs) { + // ! This mpas the albums in to its own array then its sorts the album names in reverse order let array = songs.map(song => song.album).sort((a, b) => { if(a > b) { return -1 @@ -149,6 +154,7 @@ function getAlbumsInReverseOrder(songs) { return 1 } }) + // ! This removes the duplicate album names for(let i = 0; i < array.length; i++) { if(array[i] === array[i + 1] || array[i] === array[i - 1]){ array.splice(i,1) @@ -283,15 +289,23 @@ function findFirstSongStartingWith(songs, letter) { */ function mapArtistsToSongs(songs) { + // ! With this I wanted to create an object with the key being the artist and the value being and array of the artist songs + // ! Map out an array of the artist const artists = songs.map(song => song.artist) + // ! create an object to return const mappedObject = {}; + // ! Loop through the artist array for(const artist of artists) { + // ! create an array for every artist const arr = [] + // ! Now loop through the songs array for(const song of songs) { + // ! if the artist we are currently on in the artist loop equals the current song object artist push the current song object title in the arr if(artist === song.artist) { arr.push(song.title) } } + // ! after that loop the array will have the artist songs in from the songs loop and assigin the key and value pairs if(mappedObject.hasOwnProperty(artist)){ continue; } @@ -307,7 +321,40 @@ 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 albums = songs.map(song => song.album) + const mappedObject = {}; + const average = 0 + const songAmount = 0 + const longest = 0 + const albumName = "" + for(const album of albums) { + const arr = [] + for(const song of songs) { + if(album === song.album) { + arr.push(song.runtimeInSeconds) + } + } + if(mappedObject.hasOwnProperty(album)){ + continue; + } + mappedObject[album] = arr + } + for(const album in mappedObject){ + for(const time of mappedObject[album]) { + average += time + songAmount++ + } + mappedObject[album] = Math.round(average / songAmount) + } + for(const album in mappedObject){ + if(mappedObject[album] > longest){ + longest = mappedObject[album] + albumName = album + } + } + return albumName +} // Problem #18 /** From 7ac495e5c3db06f926ab39781eeb4ebe8410abb9 Mon Sep 17 00:00:00 2001 From: llamouth Date: Tue, 9 Jan 2024 13:33:13 -0500 Subject: [PATCH 15/33] Saved new commit --- practiceLane.js | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/practiceLane.js b/practiceLane.js index 83e033e..e60cab9 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,21 +1,36 @@ -const { get } = require("http"); +const { availableParallelism } = require("os"); const exampleSongData = require("./data/songs"); -function mapArtistsToSongs(songs) { - const artists = songs.map(song => song.artist) +function findAlbumWithLongestAverageRuntime(songs) { + const albums = songs.map(song => song.album) const mappedObject = {}; - for(const artist of artists) { - const arr = [] - for(const song of songs) { - if(artist === song.artist) { - arr.push(song.title) - } + for(const album of albums) { + const arr = [] + for(const song of songs) { + if(album === song.album) { + arr.push(song.runtimeInSeconds) } - if(mappedObject.hasOwnProperty(artist)){ - continue; + } + if(mappedObject.hasOwnProperty(album)){ + continue; + } + mappedObject[album] = arr + } + for(const album in mappedObject){ + let average = 0 + let songAmount = 0 + for(const time of mappedObject[album]) { + average += time + songAmount++ + } + mappedObject[album] = Math.round(average / songAmount) + } + let longest = 0 + for(const album in mappedObject){ + if(mappedObject[album] > longest){ + longest = mappedObject[album] } - mappedObject[artist] = arr } - return mappedObject + return longest } -console.log(mapArtistsToSongs(exampleSongData)) +console.log(findAlbumWithLongestAverageRuntime(exampleSongData)) From 4a67ecca1cdfd2fdffd73760caa10b37de1a9828 Mon Sep 17 00:00:00 2001 From: llamouth Date: Tue, 9 Jan 2024 22:42:56 -0500 Subject: [PATCH 16/33] first commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e15a251..8927df9 100644 --- a/README.md +++ b/README.md @@ -75,3 +75,4 @@ - If you find yourself stuck on a problem, don't hesitate to review the [MDN Web Docs on Array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). - Collaboration is key to learning. Feel free to discuss approaches with your peers or reach out to a coach for guidance. +# Lab-Native-Array-Methods-Pt2 From 86da5abd07c9441e1c8877677bde32c43e9a4372 Mon Sep 17 00:00:00 2001 From: llamouth Date: Tue, 9 Jan 2024 22:51:03 -0500 Subject: [PATCH 17/33] Sorry I was lost during this git connecton to github but i got it now. question 19 completed --- index.js | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 1685894..4e22d7c 100644 --- a/index.js +++ b/index.js @@ -281,6 +281,7 @@ function findFirstSongStartingWith(songs, letter) { return songs.find(song => song.title.charAt(0) === letter ? song : null) } + // Problem #16 /** * Maps each artist to an array of their song titles. @@ -323,11 +324,9 @@ function mapArtistsToSongs(songs) { */ function findAlbumWithLongestAverageRuntime(songs) { const albums = songs.map(song => song.album) - const mappedObject = {}; - const average = 0 - const songAmount = 0 - const longest = 0 - const albumName = "" + let mappedObject = {}; + let longest = 0 + let albumName = "" for(const album of albums) { const arr = [] for(const song of songs) { @@ -341,11 +340,13 @@ function findAlbumWithLongestAverageRuntime(songs) { mappedObject[album] = arr } for(const album in mappedObject){ + let total = 0 + let songAmount = 0 for(const time of mappedObject[album]) { - average += time + total += time songAmount++ } - mappedObject[album] = Math.round(average / songAmount) + mappedObject[album] = Math.round(total / songAmount) } for(const album in mappedObject){ if(mappedObject[album] > longest){ @@ -361,14 +362,43 @@ function findAlbumWithLongestAverageRuntime(songs) { * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { + let sortedRunitimes = songs.sort((a, b) => a.runtimeInSeconds - b.runtimeInSeconds) + for(const song of sortedRunitimes) { + 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) { + const albums = songs.map(song => song.album) + let albumWithRuntimeObject = {}; + for(const album of albums) { + const arr = [] + for(const song of songs) { + if(album === song.album) { + arr.push(song.runtimeInSeconds) + } + } + if(albumWithRuntimeObject.hasOwnProperty(album)){ + continue; + } + albumWithRuntimeObject[album] = arr + } + for(const album in albumWithRuntimeObject){ + let songtotal = 0 + let totalRuntime = 0 + for(const time of albumWithRuntimeObject[album]) { + totalRuntime += time + songtotal++ + } + console.log(`${album}: ${songtotal} songs, Total Runtime: ${totalRuntime} seconds`) + } +} // Problem #20 /** @@ -400,4 +430,4 @@ module.exports = { printSongsSortedByRuntime, printAlbumSummaries, findArtistWithMostSongs -};; +}; From 17df5f380c533dd020dfdf74dba4480d65c2edb3 Mon Sep 17 00:00:00 2001 From: llamouth Date: Wed, 10 Jan 2024 09:46:13 -0500 Subject: [PATCH 18/33] Completed Question 20 using map method final commit --- index.js | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 4e22d7c..0dcf4f7 100644 --- a/index.js +++ b/index.js @@ -399,14 +399,37 @@ function printAlbumSummaries(songs) { console.log(`${album}: ${songtotal} songs, Total Runtime: ${totalRuntime} seconds`) } } - // 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) {} +function findArtistWithMostSongs(songs) { + const artists = songs.map(song => song.artist) + let artistWithSongsObject = {}; + for(const artist of artists) { + let totalSongs = 0 + for(const song of songs) { + if(artist === song.artist) { + totalSongs++ + } + } + if(artistWithSongsObject.hasOwnProperty(artist)){ + continue; + } + artistWithSongsObject[artist] = totalSongs + } + let mostArtist; + let highestValue = 0 + for(const key in artistWithSongsObject){ + if(artistWithSongsObject[key] > highestValue){ + mostArtist = key + highestValue = artistWithSongsObject[key] + } + } + return mostArtist +} module.exports = { @@ -430,4 +453,4 @@ module.exports = { printSongsSortedByRuntime, printAlbumSummaries, findArtistWithMostSongs -}; +};; From 4b9f5a494df137319bffeacf60e8b4f14738ad7b Mon Sep 17 00:00:00 2001 From: llamouth Date: Wed, 10 Jan 2024 11:06:04 -0500 Subject: [PATCH 19/33] Commiting to a change i did --- index.js | 8 ++++---- practiceLane.js | 36 ------------------------------------ 2 files changed, 4 insertions(+), 40 deletions(-) delete mode 100644 practiceLane.js diff --git a/index.js b/index.js index 0dcf4f7..3b28f87 100644 --- a/index.js +++ b/index.js @@ -408,6 +408,8 @@ function printAlbumSummaries(songs) { function findArtistWithMostSongs(songs) { const artists = songs.map(song => song.artist) let artistWithSongsObject = {}; + let artistWithMost; + let highestValue = 0 for(const artist of artists) { let totalSongs = 0 for(const song of songs) { @@ -420,15 +422,13 @@ function findArtistWithMostSongs(songs) { } artistWithSongsObject[artist] = totalSongs } - let mostArtist; - let highestValue = 0 for(const key in artistWithSongsObject){ if(artistWithSongsObject[key] > highestValue){ - mostArtist = key + artistWithMost = key highestValue = artistWithSongsObject[key] } } - return mostArtist + return artistWithMost } diff --git a/practiceLane.js b/practiceLane.js deleted file mode 100644 index e60cab9..0000000 --- a/practiceLane.js +++ /dev/null @@ -1,36 +0,0 @@ -const { availableParallelism } = require("os"); -const exampleSongData = require("./data/songs"); - -function findAlbumWithLongestAverageRuntime(songs) { - const albums = songs.map(song => song.album) - const mappedObject = {}; - for(const album of albums) { - const arr = [] - for(const song of songs) { - if(album === song.album) { - arr.push(song.runtimeInSeconds) - } - } - if(mappedObject.hasOwnProperty(album)){ - continue; - } - mappedObject[album] = arr - } - for(const album in mappedObject){ - let average = 0 - let songAmount = 0 - for(const time of mappedObject[album]) { - average += time - songAmount++ - } - mappedObject[album] = Math.round(average / songAmount) - } - let longest = 0 - for(const album in mappedObject){ - if(mappedObject[album] > longest){ - longest = mappedObject[album] - } - } - return longest -} -console.log(findAlbumWithLongestAverageRuntime(exampleSongData)) From db626d9da4b2addd1a489375a53bbba259763847 Mon Sep 17 00:00:00 2001 From: llamouth Date: Thu, 11 Jan 2024 22:14:18 -0500 Subject: [PATCH 20/33] Refactoring My Code --- README.md | 78 ----------------------- index.js | 163 ++++++++++++++++++++++-------------------------- practiceLane.js | 12 ++++ 3 files changed, 85 insertions(+), 168 deletions(-) delete mode 100644 README.md create mode 100644 practiceLane.js diff --git a/README.md b/README.md deleted file mode 100644 index 8927df9..0000000 --- a/README.md +++ /dev/null @@ -1,78 +0,0 @@ -# ๐Ÿš€ 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()`. - -## Getting Started - -### 1. ๐Ÿด Clone the Repository (from Github) -- On the GitHub page of this repository, click the green "Code" button. -- Copy the URL provided under "Clone with HTTPS". -- Open your terminal and execute: - ``` - git clone [paste the URL here] - ``` -- This command clones the repository to your local machine. - -### 1a. ๐Ÿด Clone the Repository (without Github) - -1. **Clone the Repository Directly:** - - Open your terminal on your local machine. - -2. **Clone Using Terminal:** - - Execute the following command to clone the repository (replace `[paste the URL here]` with the URL you provided): - ``` - git clone https://github.com/CastonPursuit/Lab-Native-Array-Methods-Pt2.git - ``` - - This command will clone the repository to your local machine. - - -### 2. ๐Ÿ” Explore the Problems -- In your terminal, navigate to the cloned repository's directory: - ``` - cd Lab-Native-Array-Methods-Pt2 - ``` -- Open the project in VS Code by typing: - ``` - code . - ``` -- You will find the `index.js` file containing 20 problems, each using a different array method. - -### 3. ๐Ÿ’ก Work on the Problems -- Each problem in `index.js` focuses on practicing a specific array method. -- Read the instructions for each problem and try to solve it. -- Use `console.log()` within your solutions to output results. - -### 4. ๐Ÿƒ Run Your Code -- Test your solutions by running: - ``` - node index.js - ``` -- This command executes your code in `index.js`, allowing you to see the output in the terminal. - -### 5. ๐Ÿงช Test Your Solutions -- To ensure your solutions are correct, use: - ``` - npm test - ``` -- This will run any predefined tests to validate your solutions. - -### 6. ๐Ÿ’พ Save Your Progress -- After solving each problem and verifying your solution, commit your progress: - ``` - git add . - git commit -m 'Completed problem # using Y method' - ``` -- Replace `#` with the problem number and `Y` with the array method used. - -### 7. ๐Ÿ”„ Repeat -- Continue working through each problem, running your code, testing, and committing your solutions. - -### 8. ๐Ÿš€ Next Steps -- After completing all the problems, you're ready for the next phase of your learning journey. -- In the upcoming lesson, you'll learn how to push your solutions back to GitHub, sharing your progress and receiving feedback. - -### 9. ๐Ÿ†˜ Need Help? -- If you find yourself stuck on a problem, don't hesitate to review the [MDN Web Docs on Array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). -- Collaboration is key to learning. Feel free to discuss approaches with your peers or reach out to a coach for guidance. - -# Lab-Native-Array-Methods-Pt2 diff --git a/index.js b/index.js index 3b28f87..c116534 100644 --- a/index.js +++ b/index.js @@ -42,29 +42,16 @@ function getSongsFromAlbum(songs, albumName) { * @returns {Object} An object with counts of short, medium, and long songs. */ function categorizeSongsByRuntime(songs) { - let shortSongs = songs.reduce((total, song) => { + return songs.reduce((runtimeObject, song) => { if(song.runtimeInSeconds < 180) { - total++ + runtimeObject.shortSongs++ + }else if(song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300) { + runtimeObject.mediumSongs++ + }else if(song.runtimeInSeconds > 300) { + runtimeObject.longSongs++ } - return total - }, 0) - let mediumSongs = songs.reduce((total, song) => { - if(song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300) { - total++ - } - return total - }, 0) - let longSongs = songs.reduce((total, song) => { - if(song.runtimeInSeconds > 300) { - total++ - } - return total - }, 0) - return { - shortSongs: shortSongs, - mediumSongs: mediumSongs, - longSongs: longSongs - } + return runtimeObject + }, {shortSongs: 0, mediumSongs: 0, longSongs: 0}) } // #4 @@ -74,36 +61,21 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { - // ! I wanted to create an object with the key being the album name and the values being the amount of songs on the album let albums = songs.map(song => song.album) - let obj = {} let mostSongsValue = 0 let album; - // ! This loop is creating key(album) value(0) pairs in the obj variable. if the key is already created it continues in the loop so there is no duplicates - for(let i = 0; i < songs.length; i++) { - if(obj.hasOwnProperty(songs[i].album)){ - continue; - } - obj[songs[i].album] = 0 - } - // ! These loops is counting the songs with the same album name to give values in the obj variable - for(let j = 0; j < albums.length; j++) { - for(let key in obj) { - if(albums[j] === key){ - obj[key]++ - } - } - } - // ! this loop is assigning the album with the most songs to return the album with the most songs - for(let key in obj) { - if(obj[key] > mostSongsValue) { - mostSongsValue = obj[key] - album = key + albums.reduce((albumsObject, currAlbum) => { + albumsObject[currAlbum] = (albumsObject[currAlbum] || 0) + 1 + if(albumsObject[currAlbum] > mostSongsValue) { + mostSongsValue = albumsObject[currAlbum] + album = currAlbum } - } + return albumsObject + },{}) return album } + // #5 /** * Returns details of the first song in a specific album. @@ -147,13 +119,13 @@ function getSongsWithDurationInMinutes(songs) { */ function getAlbumsInReverseOrder(songs) { // ! This mpas the albums in to its own array then its sorts the album names in reverse order - let array = songs.map(song => song.album).sort((a, b) => { + let array = songs.map(song => song.album).sort((a, b) => { if(a > b) { return -1 }else { return 1 } - }) + }); // ! This removes the duplicate album names for(let i = 0; i < array.length; i++) { if(array[i] === array[i + 1] || array[i] === array[i - 1]){ @@ -171,12 +143,7 @@ function getAlbumsInReverseOrder(songs) { * @returns {string[]} An array of song titles containing the word. */ function songsWithWord(songs, word) { - let array = [] - let newArr = songs.filter(song => song.title.includes(word)) - for(const obj of newArr){ - array.push(obj.title) - } - return array + return songs.filter(song => song.title.includes("Berlin")).map(song => song.title); } @@ -203,25 +170,32 @@ function getTotalRuntimeOfArtist(songs, artistName) { */ function printArtistsWithMultipleSongs(songs) { let artist = songs.map(song => song.artist) - let obj = {} - for(let i = 0; i < songs.length; i++) { - if(obj.hasOwnProperty(songs[i].artist)){ - continue; - } - obj[songs[i].artist] = 0 - } - for(let j = 0; j < artist.length; j++) { - for(let key in obj) { - if(artist[j] === key){ - obj[key]++ - } - } - } - for(let key in obj) { - if(obj[key] > 1 ) { - console.log(key) + return artist.reduce((artistObject, currArtist) => { + artistObject[currArtist] = (artistObject[currArtist] || 0) + 1 + if(artistObject[currArtist] > 1) { + console.log(currArtist) } - } + return artistObject + },{}) + // let obj = {} + // for(let i = 0; i < songs.length; i++) { + // if(obj.hasOwnProperty(songs[i].artist)){ + // continue; + // } + // obj[songs[i].artist] = 0 + // } + // for(let j = 0; j < artist.length; j++) { + // for(let key in obj) { + // if(artist[j] === key){ + // obj[key]++ + // } + // } + // } + // for(let key in obj) { + // if(obj[key] > 1 ) { + // console.log(key) + // } + // } } // Problem #12 @@ -290,7 +264,7 @@ function findFirstSongStartingWith(songs, letter) { */ function mapArtistsToSongs(songs) { - // ! With this I wanted to create an object with the key being the artist and the value being and array of the artist songs + // ! With this I wanted to create an object with the key being the artist and the value being an array of the artist songs // ! Map out an array of the artist const artists = songs.map(song => song.artist) // ! create an object to return @@ -406,29 +380,38 @@ function printAlbumSummaries(songs) { * @returns {string} The name of the artist with the most songs. */ function findArtistWithMostSongs(songs) { - const artists = songs.map(song => song.artist) - let artistWithSongsObject = {}; + let artists = songs.map(song => song.artist) + // let artistWithSongsObject = {}; let artistWithMost; let highestValue = 0 - for(const artist of artists) { - let totalSongs = 0 - for(const song of songs) { - if(artist === song.artist) { - totalSongs++ - } - } - if(artistWithSongsObject.hasOwnProperty(artist)){ - continue; + artists.reduce((artistObject, currArtist) => { + artistObject[currArtist] = (artistObject[currArtist] || 0) + 1 + if(highestValue < artistObject[currArtist]) { + highestValue = artistObject[currArtist] + artistWithMost = currArtist } - artistWithSongsObject[artist] = totalSongs - } - for(const key in artistWithSongsObject){ - if(artistWithSongsObject[key] > highestValue){ - artistWithMost = key - highestValue = artistWithSongsObject[key] - } - } + return artistObject + },{}) return artistWithMost + // for(const artist of artists) { + // let totalSongs = 0 + // for(const song of songs) { + // if(artist === song.artist) { + // totalSongs++ + // } + // } + // if(artistWithSongsObject.hasOwnProperty(artist)){ + // continue; + // } + // artistWithSongsObject[artist] = totalSongs + // } + // for(const key in artistWithSongsObject){ + // if(artistWithSongsObject[key] > highestValue){ + // artistWithMost = key + // highestValue = artistWithSongsObject[key] + // } + // } + // return artistWithMost } diff --git a/practiceLane.js b/practiceLane.js new file mode 100644 index 0000000..b54f472 --- /dev/null +++ b/practiceLane.js @@ -0,0 +1,12 @@ +const exampleSongData = require("./data/songs"); + +function songsWithWord(songs, word) { + let array = [] + let newArr = songs.filter(song => song.title.includes(word)) + for(const obj of newArr){ + array.push(obj.title) + } + return array + } + + console.log(songsWithWord(exampleSongData)) \ No newline at end of file From 2982e689154848bcd2707e59bd7fa99de612d282 Mon Sep 17 00:00:00 2001 From: llamouth Date: Fri, 12 Jan 2024 09:17:15 -0500 Subject: [PATCH 21/33] Refactored Code --- index.js | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/index.js b/index.js index c116534..62db146 100644 --- a/index.js +++ b/index.js @@ -235,11 +235,7 @@ function sortSongsByArtistAndTitle(songs) { */ function listAlbumTotalRuntimes(songs) { return songs.reduce((obj, song) => { - if(obj.hasOwnProperty(song.album)) { - obj[song.album] += song.runtimeInSeconds - }else { - obj[song.album] = song.runtimeInSeconds - } + obj[song.album] = (obj[song.album] || 0) + song.runtimeInSeconds return obj; },{}) } @@ -264,23 +260,15 @@ function findFirstSongStartingWith(songs, letter) { */ function mapArtistsToSongs(songs) { - // ! With this I wanted to create an object with the key being the artist and the value being an array of the artist songs - // ! Map out an array of the artist const artists = songs.map(song => song.artist) - // ! create an object to return const mappedObject = {}; - // ! Loop through the artist array for(const artist of artists) { - // ! create an array for every artist const arr = [] - // ! Now loop through the songs array for(const song of songs) { - // ! if the artist we are currently on in the artist loop equals the current song object artist push the current song object title in the arr if(artist === song.artist) { arr.push(song.title) } } - // ! after that loop the array will have the artist songs in from the songs loop and assigin the key and value pairs if(mappedObject.hasOwnProperty(artist)){ continue; } From 86448ab5c60e3b37b58be2f4ab5bf6c499e9c9ff Mon Sep 17 00:00:00 2001 From: llamouth Date: Fri, 12 Jan 2024 10:49:53 -0500 Subject: [PATCH 22/33] Refactoring Code --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 62db146..a27b19a 100644 --- a/index.js +++ b/index.js @@ -248,7 +248,7 @@ function listAlbumTotalRuntimes(songs) { * @returns {Object|null} The first song object that matches the criterion or null. */ function findFirstSongStartingWith(songs, letter) { - return songs.find(song => song.title.charAt(0) === letter ? song : null) + return songs.find(song => song.title[0] === letter) || null; } @@ -310,8 +310,8 @@ function findAlbumWithLongestAverageRuntime(songs) { } mappedObject[album] = Math.round(total / songAmount) } - for(const album in mappedObject){ - if(mappedObject[album] > longest){ + for(const album in mappedObject) { + if(mappedObject[album] > longest) { longest = mappedObject[album] albumName = album } From 52db116c76545ec738960778c85c7ab3b5c2f6eb Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Fri, 12 Jan 2024 16:26:30 -0500 Subject: [PATCH 23/33] Refactored my code --- index.js | 102 ++++++++++++++++++++++++++---------------------- practiceLane.js | 19 +++++---- 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/index.js b/index.js index a27b19a..a9374cd 100644 --- a/index.js +++ b/index.js @@ -105,10 +105,8 @@ function isThereLongSong(songs, runtime) { * @returns {Object[]} Array of song objects with runtime in minutes. */ function getSongsWithDurationInMinutes(songs) { - for(let i = 0; i < songs.length; i++) { - songs[i].durationInMinutes = songs[i].runtimeInSeconds / 60 - } - return songs; + songs.forEach(song => song["durationInMinutes"] = song.runtimeInSeconds / 60) + return songs } // #8 @@ -260,21 +258,17 @@ function findFirstSongStartingWith(songs, letter) { */ function mapArtistsToSongs(songs) { - const artists = songs.map(song => song.artist) - const mappedObject = {}; - for(const artist of artists) { - const arr = [] - for(const song of songs) { - if(artist === song.artist) { - arr.push(song.title) + // * MY CODE + return songs.reduce((object, song) => { + let arr = [] + for(const songs2 of songs){ + if(song.artist === songs2.artist){ + arr.push(songs2.title) } } - if(mappedObject.hasOwnProperty(artist)){ - continue; - } - mappedObject[artist] = arr - } - return mappedObject + object[song.artist] = (object[song.artist] || arr) + return object + },{}) } @@ -296,10 +290,7 @@ function findAlbumWithLongestAverageRuntime(songs) { arr.push(song.runtimeInSeconds) } } - if(mappedObject.hasOwnProperty(album)){ - continue; - } - mappedObject[album] = arr + mappedObject[album] = (mappedObject[album] ||arr) } for(const album in mappedObject){ let total = 0 @@ -325,10 +316,8 @@ function findAlbumWithLongestAverageRuntime(songs) { * @param {Object[]} songs - An array of songs. */ function printSongsSortedByRuntime(songs) { - let sortedRunitimes = songs.sort((a, b) => a.runtimeInSeconds - b.runtimeInSeconds) - for(const song of sortedRunitimes) { - console.log(song.title) - } + songs.sort((a, b) => a.runtimeInSeconds - b.runtimeInSeconds) + .forEach((song) => console.log(song.title)) } // Problem #19 @@ -337,30 +326,51 @@ function printSongsSortedByRuntime(songs) { * @param {Object[]} songs - An array of songs. */ function printAlbumSummaries(songs) { - const albums = songs.map(song => song.album) - let albumWithRuntimeObject = {}; - for(const album of albums) { - const arr = [] - for(const song of songs) { - if(album === song.album) { - arr.push(song.runtimeInSeconds) - } - } - if(albumWithRuntimeObject.hasOwnProperty(album)){ - continue; - } - albumWithRuntimeObject[album] = arr - } - for(const album in albumWithRuntimeObject){ - let songtotal = 0 - let totalRuntime = 0 - for(const time of albumWithRuntimeObject[album]) { - totalRuntime += time - songtotal++ + // * CLASSCODE + 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; } - console.log(`${album}: ${songtotal} songs, Total Runtime: ${totalRuntime} seconds`) + }) + for(const summary in albumSummaries) { + console.log(`${albumSummaries[summary].albumName}: ${albumSummaries[summary].songCount} songs, Total Runtime: ${albumSummaries[summary].totalRuntime} seconds`) + // To account for the s in a plural value ^ ${albumSummaries[summary].songCount > 1 ? "s" : ""} } + + // * MYCODE + // const albums = songs.map(song => song.album) + // let albumWithRuntimeObject = {}; + // for(const album of albums) { + // const arr = [] + // for(const song of songs) { + // if(album === song.album) { + // arr.push(song.runtimeInSeconds) + // } + // } + // if(albumWithRuntimeObject.hasOwnProperty(album)){ + // continue; + // } + // albumWithRuntimeObject[album] = arr + // } + // for(const album in albumWithRuntimeObject){ + // let songtotal = 0 + // let totalRuntime = 0 + // for(const time of albumWithRuntimeObject[album]) { + // totalRuntime += time + // songtotal++ + // } + // console.log(`${album}: ${songtotal} songs, Total Runtime: ${totalRuntime} seconds`) + // } } + // Problem #20 /** * Finds the artist with the most songs in the list. diff --git a/practiceLane.js b/practiceLane.js index b54f472..dc95576 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,12 +1,11 @@ const exampleSongData = require("./data/songs"); -function songsWithWord(songs, word) { - let array = [] - let newArr = songs.filter(song => song.title.includes(word)) - for(const obj of newArr){ - array.push(obj.title) - } - return array - } - - console.log(songsWithWord(exampleSongData)) \ No newline at end of file +function getSongsWithDurationInMinutes(songs) { + songs.forEach(song => song["durationInMinutes"] = song.runtimeInSeconds / 60) + return songs + // for(let i = 0; i < songs.length; i++) { + // songs[i].durationInMinutes = songs[i].runtimeInSeconds / 60 + // } + // return songs; +} + console.log(getSongsWithDurationInMinutes(exampleSongData)) \ No newline at end of file From 315f49f9a51e297c4536ce6df22904d5b8bdee44 Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Fri, 12 Jan 2024 16:55:25 -0500 Subject: [PATCH 24/33] Refactoring --- index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index a9374cd..90acdcd 100644 --- a/index.js +++ b/index.js @@ -124,13 +124,8 @@ function getAlbumsInReverseOrder(songs) { return 1 } }); - // ! This removes the duplicate album names - for(let i = 0; i < array.length; i++) { - if(array[i] === array[i + 1] || array[i] === array[i - 1]){ - array.splice(i,1) - } - } - return array + return array.filter((item, + index) => array.indexOf(item) === index) } // #9 From da994c7ad509a688e2921d605542651a4475fa37 Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sat, 13 Jan 2024 00:28:14 -0500 Subject: [PATCH 25/33] Refactoring Code --- index.js | 11 ++--------- practiceLane.js | 11 +++-------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 90acdcd..b83fd1a 100644 --- a/index.js +++ b/index.js @@ -25,13 +25,7 @@ function getSortedTitles(songs) { * @returns {string[]} An array of song titles. */ function getSongsFromAlbum(songs, albumName) { - let newArr = []; - for(let i = 0; i < songs.length; i++){ - if(songs[i].album === albumName){ - newArr.push(songs[i].title) - } - } - return newArr + return songs.map(song => song.album === albumName ? song.title : "").filter(Boolean) } @@ -116,7 +110,6 @@ function getSongsWithDurationInMinutes(songs) { * @returns {string[]} Array of album names in reverse alphabetical order. */ function getAlbumsInReverseOrder(songs) { - // ! This mpas the albums in to its own array then its sorts the album names in reverse order let array = songs.map(song => song.album).sort((a, b) => { if(a > b) { return -1 @@ -136,7 +129,7 @@ function getAlbumsInReverseOrder(songs) { * @returns {string[]} An array of song titles containing the word. */ function songsWithWord(songs, word) { - return songs.filter(song => song.title.includes("Berlin")).map(song => song.title); + return songs.filter(song => song.title.includes(word)).map(song => song.title); } diff --git a/practiceLane.js b/practiceLane.js index dc95576..c6a1b30 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,11 +1,6 @@ const exampleSongData = require("./data/songs"); -function getSongsWithDurationInMinutes(songs) { - songs.forEach(song => song["durationInMinutes"] = song.runtimeInSeconds / 60) - return songs - // for(let i = 0; i < songs.length; i++) { - // songs[i].durationInMinutes = songs[i].runtimeInSeconds / 60 - // } - // return songs; +function getSongsFromAlbum(songs, albumName) { + return songs.map(song => song.album === albumName ? song.title : "").filter(Boolean) } - console.log(getSongsWithDurationInMinutes(exampleSongData)) \ No newline at end of file + console.log(getSongsFromAlbum(exampleSongData,"Seasonal Sounds")) \ No newline at end of file From 6aa590ce6e60a0b3ba26bb281d7332ef4cba5110 Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:28:05 -0500 Subject: [PATCH 26/33] Refactoring Code --- index.js | 43 +++++++++++++++++++------------------------ practiceLane.js | 28 +++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index b83fd1a..316178f 100644 --- a/index.js +++ b/index.js @@ -110,15 +110,15 @@ function getSongsWithDurationInMinutes(songs) { * @returns {string[]} Array of album names in reverse alphabetical order. */ function getAlbumsInReverseOrder(songs) { - let array = songs.map(song => song.album).sort((a, b) => { + let albumArray = songs.map(song => song.album).sort((a, b) => { if(a > b) { return -1 }else { return 1 } }); - return array.filter((item, - index) => array.indexOf(item) === index) + return albumArray.filter((album, + index) => albumArray.indexOf(album) === index) } // #9 @@ -266,32 +266,27 @@ function mapArtistsToSongs(songs) { * @param {Object[]} songs - An array of songs. * @returns {string} The name of the album with the longest average song runtime. */ +// object[song.album] = (object[song.album] || r) += song.runtimeInSeconds && object[song.album].songCount++ + function findAlbumWithLongestAverageRuntime(songs) { - const albums = songs.map(song => song.album) - let mappedObject = {}; - let longest = 0 + let longestAvg = 0 let albumName = "" - for(const album of albums) { - const arr = [] - for(const song of songs) { - if(album === song.album) { - arr.push(song.runtimeInSeconds) + let mappedObject = songs.reduce((object, song) => { + if(!object[song.album]){ + object[song.album] = { + runtimes: song.runtimeInSeconds, + songCount: 1 } + }else { + object[song.album].runtimes += song.runtimeInSeconds + object[song.album].songCount++ } - mappedObject[album] = (mappedObject[album] ||arr) - } - for(const album in mappedObject){ - let total = 0 - let songAmount = 0 - for(const time of mappedObject[album]) { - total += time - songAmount++ - } - mappedObject[album] = Math.round(total / songAmount) - } + return object + },{}) for(const album in mappedObject) { - if(mappedObject[album] > longest) { - longest = mappedObject[album] + let average = mappedObject[album].runtimes / mappedObject[album].songCount + if(average > longestAvg) { + longestAvg = average albumName = album } } diff --git a/practiceLane.js b/practiceLane.js index c6a1b30..653ce2b 100644 --- a/practiceLane.js +++ b/practiceLane.js @@ -1,6 +1,28 @@ const exampleSongData = require("./data/songs"); -function getSongsFromAlbum(songs, albumName) { - return songs.map(song => song.album === albumName ? song.title : "").filter(Boolean) +function findAlbumWithLongestAverageRuntime(songs) { + let longest = 0 + let albumName = "" + let mappedObject = songs.reduce((object, song) => { + if(!object[song.album]){ + object[song.album] = { + runtimes: song.runtimeInSeconds, + songCount: 1 + } + }else { + object[song.album].runtimes += song.runtimeInSeconds + object[song.album].songCount++ + } + return object + },{}) + for(const album in mappedObject) { + let average = mappedObject[album].runtimes / mappedObject[album].songCount + if(average > longest) { + longest = average + albumName = album + } + } + return albumName } - console.log(getSongsFromAlbum(exampleSongData,"Seasonal Sounds")) \ No newline at end of file + + console.log(findAlbumWithLongestAverageRuntime(exampleSongData)) \ No newline at end of file From 137a0cc0de7158a357714dab47e1c84340bad55b Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sat, 13 Jan 2024 10:39:33 -0500 Subject: [PATCH 27/33] Final Commit... ithink --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 316178f..6856f51 100644 --- a/index.js +++ b/index.js @@ -25,7 +25,7 @@ function getSortedTitles(songs) { * @returns {string[]} An array of song titles. */ function getSongsFromAlbum(songs, albumName) { - return songs.map(song => song.album === albumName ? song.title : "").filter(Boolean) + return songs.map(song => song.album === albumName ? song.title : null).filter(Boolean) } From 37abaa6abbf30f81b5eedaeddaa0e840591723d6 Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sun, 14 Jan 2024 12:00:32 -0500 Subject: [PATCH 28/33] Final Final Commit --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 6856f51..aab092e 100644 --- a/index.js +++ b/index.js @@ -271,7 +271,7 @@ function mapArtistsToSongs(songs) { function findAlbumWithLongestAverageRuntime(songs) { let longestAvg = 0 let albumName = "" - let mappedObject = songs.reduce((object, song) => { + songs.reduce((object, song) => { if(!object[song.album]){ object[song.album] = { runtimes: song.runtimeInSeconds, @@ -281,15 +281,13 @@ function findAlbumWithLongestAverageRuntime(songs) { object[song.album].runtimes += song.runtimeInSeconds object[song.album].songCount++ } - return object - },{}) - for(const album in mappedObject) { - let average = mappedObject[album].runtimes / mappedObject[album].songCount + let average = object[song.album].runtimes / object[song.album].songCount if(average > longestAvg) { longestAvg = average - albumName = album + albumName = song.album } - } + return object + },{}) return albumName } From a2722764c50cd761eaf1738a09de6f1db9c93a2a Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sun, 14 Jan 2024 12:21:59 -0500 Subject: [PATCH 29/33] Commiting cause it says i have to --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index aab092e..ba34860 100644 --- a/index.js +++ b/index.js @@ -288,7 +288,7 @@ function findAlbumWithLongestAverageRuntime(songs) { } return object },{}) - return albumName + return albumName; } // Problem #18 From f1da75753d864e86f78f85b167017f87493b4b89 Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sun, 21 Jan 2024 14:22:30 -0500 Subject: [PATCH 30/33] Refactored Code --- index.js | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/index.js b/index.js index ba34860..602457b 100644 --- a/index.js +++ b/index.js @@ -117,8 +117,7 @@ function getAlbumsInReverseOrder(songs) { return 1 } }); - return albumArray.filter((album, - index) => albumArray.indexOf(album) === index) + return albumArray.filter((album, index) => albumArray.indexOf(album) === index) } // #9 @@ -211,8 +210,6 @@ function sortSongsByArtistAndTitle(songs) { }) } - - // Problem #14 /** * Lists albums along with their total runtime. @@ -237,7 +234,6 @@ function findFirstSongStartingWith(songs, letter) { return songs.find(song => song.title[0] === letter) || null; } - // Problem #16 /** * Maps each artist to an array of their song titles. @@ -259,7 +255,6 @@ function mapArtistsToSongs(songs) { },{}) } - // Problem #17 /** * Finds the album with the longest average song runtime. @@ -393,7 +388,6 @@ function findArtistWithMostSongs(songs) { // return artistWithMost } - module.exports = { getSortedTitles, getSongsFromAlbum, From b1e407890d4027672fee124e89c2b341b697b56e Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Sun, 21 Jan 2024 21:55:37 -0500 Subject: [PATCH 31/33] Refactor Code --- index.js | 88 +++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/index.js b/index.js index 602457b..17679f2 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ Native Array Methods pt.2 continues with the same dataset: songs. All required f */ +const { ECDH } = require("crypto"); const exampleSongData = require("./data/songs"); // Do not change the line above. @@ -243,18 +244,20 @@ function findFirstSongStartingWith(songs, letter) { function mapArtistsToSongs(songs) { // * MY CODE - return songs.reduce((object, song) => { + let mappedObject = songs.reduce((object, song) => { let arr = [] - for(const songs2 of songs){ - if(song.artist === songs2.artist){ - arr.push(songs2.title) - } - } - object[song.artist] = (object[song.artist] || arr) + arr.push(song.title) + object[song.artist] = (object[song.artist] || arr).concat(arr) return object },{}) + for(const key in mappedObject) { + mappedObject[key] = mappedObject[key].filter((songs, i) => mappedObject[key].indexOf(songs) === i) + } + return mappedObject } +console.log(mapArtistsToSongs(exampleSongData)) + // Problem #17 /** * Finds the album with the longest average song runtime. @@ -303,48 +306,41 @@ function printSongsSortedByRuntime(songs) { */ function printAlbumSummaries(songs) { // * CLASSCODE - let albumSummaries = {}; - songs.forEach(song => { - if(!albumSummaries[song.album]){ - albumSummaries[song.album] = { - albumName: song.album, - songCount: 1, - totalRuntime: song.runtimeInSeconds - } + // 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 in albumSummaries) { + // console.log(`${albumSummaries[summary].albumName}: ${albumSummaries[summary].songCount} songs, Total Runtime: ${albumSummaries[summary].totalRuntime} seconds`) + // // To account for the s in a plural value ^ ${albumSummaries[summary].songCount > 1 ? "s" : ""} + // } + + // * MYCODE + let object = songs.reduce((obj, song) => { + if(!obj[song.album]) { + obj[song.album] = { + albumName : song.album, + songCount : 1, + totalRuntime : song.runtimeInSeconds + } }else { - albumSummaries[song.album].songCount++; - albumSummaries[song.album].totalRuntime += song.runtimeInSeconds; + obj[song.album].songCount++ + obj[song.album].totalRuntime += song.runtimeInSeconds } - }) - for(const summary in albumSummaries) { - console.log(`${albumSummaries[summary].albumName}: ${albumSummaries[summary].songCount} songs, Total Runtime: ${albumSummaries[summary].totalRuntime} seconds`) - // To account for the s in a plural value ^ ${albumSummaries[summary].songCount > 1 ? "s" : ""} + return obj + },{}) + for(const album in object){ + console.log(`${object[album].albumName}: ${object[album].songCount} songs, Total Runtime: ${object[album].totalRuntime} seconds`) } - - // * MYCODE - // const albums = songs.map(song => song.album) - // let albumWithRuntimeObject = {}; - // for(const album of albums) { - // const arr = [] - // for(const song of songs) { - // if(album === song.album) { - // arr.push(song.runtimeInSeconds) - // } - // } - // if(albumWithRuntimeObject.hasOwnProperty(album)){ - // continue; - // } - // albumWithRuntimeObject[album] = arr - // } - // for(const album in albumWithRuntimeObject){ - // let songtotal = 0 - // let totalRuntime = 0 - // for(const time of albumWithRuntimeObject[album]) { - // totalRuntime += time - // songtotal++ - // } - // console.log(`${album}: ${songtotal} songs, Total Runtime: ${totalRuntime} seconds`) - // } } // Problem #20 From 338c4604b8786dda083833c449fe691855dc7f0a Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Mon, 22 Jan 2024 08:39:47 -0500 Subject: [PATCH 32/33] Refactor Code --- index.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 17679f2..d9d8ff8 100644 --- a/index.js +++ b/index.js @@ -244,16 +244,12 @@ function findFirstSongStartingWith(songs, letter) { function mapArtistsToSongs(songs) { // * MY CODE - let mappedObject = songs.reduce((object, song) => { + return songs.reduce((object, song) => { let arr = [] arr.push(song.title) - object[song.artist] = (object[song.artist] || arr).concat(arr) + object[song.artist] = (object[song.artist] || []).concat(arr) return object },{}) - for(const key in mappedObject) { - mappedObject[key] = mappedObject[key].filter((songs, i) => mappedObject[key].indexOf(songs) === i) - } - return mappedObject } console.log(mapArtistsToSongs(exampleSongData)) From 9d3c7c66c5a0795d5c7a7aecd818661898a94539 Mon Sep 17 00:00:00 2001 From: llamouth <150949923+llamouth@users.noreply.github.com> Date: Mon, 22 Jan 2024 10:03:17 -0500 Subject: [PATCH 33/33] Refactored Code --- index.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/index.js b/index.js index d9d8ff8..1ec1175 100644 --- a/index.js +++ b/index.js @@ -252,8 +252,6 @@ function mapArtistsToSongs(songs) { },{}) } -console.log(mapArtistsToSongs(exampleSongData)) - // Problem #17 /** * Finds the album with the longest average song runtime.