From 42f68e57d048a6c1a201556d8d416e3c215fc766 Mon Sep 17 00:00:00 2001 From: ViaticusRex Date: Thu, 11 Jan 2024 11:21:04 -0500 Subject: [PATCH 1/6] Copied over my answers from the first cloned version of the project. --- index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..89fa8ec 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,12 @@ const exampleSongData = require("./data/songs"); * @param {Object[]} songs - An array of songs. * @returns {string[]} Sorted song titles. */ -function getSortedTitles(songs) {} +function getSortedTitles(songs) { + return songs.map(x => x.title).sort(); +} + +console.log(getSortedTitles(exampleSongData)); + // #2 /** @@ -22,7 +27,9 @@ 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(songByAlbum => songByAlbum.album === albumName).map(songArr => songArr.title) +} // #3 /** From 54b249b572b95f0e032f35eda8c902f034ef65ba Mon Sep 17 00:00:00 2001 From: ViaticusRex Date: Fri, 12 Jan 2024 10:51:24 -0500 Subject: [PATCH 2/6] Finished question 15 with Coach Caston during in-class session --- index.js | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 89fa8ec..6faa392 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 @@ -37,7 +37,23 @@ 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) { + const songLength = {shortSongs: 0 , mediumSongs: 0,longSongs: 0 } + + songs.forEach(song => { + if (song.runtimeInSeconds < 180){ + songLength.shortSongs++ + } else if (song.runtimeInSeconds >= 180 && song.runtimeInSeconds < 240){ + songLength.mediumSongs++ + } else { + songLength.longSongs++ + } + }) + + return songLength + + +} // #4 /** @@ -45,7 +61,28 @@ 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) { + const albumWithMost = songs.reduce((acc,song) => { + if (acc[song.album]) { + acc[song.album]++ + } else { + acc[song.album] = 1 + } + return acc + + }, {}); + + let count = 0 + let result = '' + for (const album in albumWithMost) { + if(albumWithMost[album] > count) { + count = albumWithMost[album] + result = album + } + return result + } +} + // #5 /** @@ -136,7 +173,19 @@ 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) { + + const firstSongWithLetter = songs.find(song => (song.title[0] === letter)) + return firstSongWithLetter + + + + +} + + +console.log(findFirstSongStartingWith(exampleSongData, "B")); + // Problem #16 /** @@ -144,7 +193,14 @@ 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 /** From bd902ae73793da26db44532bbd508b6a696aed55 Mon Sep 17 00:00:00 2001 From: ViaticusRex Date: Fri, 12 Jan 2024 11:57:32 -0500 Subject: [PATCH 3/6] Finished 16 with Coach Caston during in-class session. --- index.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 6faa392..4813285 100644 --- a/index.js +++ b/index.js @@ -194,14 +194,22 @@ console.log(findFirstSongStartingWith(exampleSongData, "B")); * @returns {Object} An object mapping each artist to an array of their song titles. */ function mapArtistsToSongs(songs) { - - - - - + let artistMap = {}; + songs.map(song => { + if (!artistMap.hasOwnProperty(song.artist)){ + artistMap[song.artist] = [song.title] + } + else { + artistMap[song.artist].push(song.title) + } + }) + return artistMap + } +mapArtistsToSongs(exampleSongData); + // Problem #17 /** * Finds the album with the longest average song runtime. From b1f8cafb1d0c24eed99a3b85721d658f76047e34 Mon Sep 17 00:00:00 2001 From: ViaticusRex Date: Mon, 15 Jan 2024 13:16:53 -0500 Subject: [PATCH 4/6] I completed #5 after countless errors with the testing suite. I was unable to see if I was passing or not until I erased some of my other functions - even the in class sessions. --- index.js | 94 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 4813285..9fc66ee 100644 --- a/index.js +++ b/index.js @@ -14,11 +14,9 @@ const exampleSongData = require("./data/songs"); * @returns {string[]} Sorted song titles. */ function getSortedTitles(songs) { - return songs.map(x => x.title).sort(); + return songs.map(song => song.title).sort(); } -// console.log(getSortedTitles(exampleSongData)); - // #2 /** @@ -62,25 +60,25 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { - const albumWithMost = songs.reduce((acc,song) => { - if (acc[song.album]) { - acc[song.album]++ - } else { - acc[song.album] = 1 - } - return acc + // const albumWithMost = songs.reduce((acc,song) => { + // if (acc[song.album]) { + // acc[song.album]++ + // } else { + // acc[song.album] = 1 + // } + // return acc - }, {}); + // }, {}); - let count = 0 - let result = '' - for (const album in albumWithMost) { - if(albumWithMost[album] > count) { - count = albumWithMost[album] - result = album - } - return result - } + // let count = 0 + // let result = '' + // for (const song in albumWithMost) { + // if(albumWithMost[song] > count) { + // count = albumWithMost[song]++ + // result = song.album + // } + // return result + // } } @@ -91,7 +89,10 @@ 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) { + const findFirstSong = songs.find(song => song.album === albumName); + return findFirstSong || null ; +} // #6 /** @@ -173,20 +174,10 @@ 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) { - - const firstSongWithLetter = songs.find(song => (song.title[0] === letter)) - return firstSongWithLetter +function findFirstSongStartingWith(songs, letter) {} - -} - - -console.log(findFirstSongStartingWith(exampleSongData, "B")); - - // Problem #16 /** * Maps each artist to an array of their song titles. @@ -208,7 +199,6 @@ function mapArtistsToSongs(songs) { } -mapArtistsToSongs(exampleSongData); // Problem #17 /** @@ -216,14 +206,50 @@ mapArtistsToSongs(exampleSongData); * @param {Object[]} songs - An array of songs. * @returns {string} The name of the album with the longest average song runtime. */ + + function findAlbumWithLongestAverageRuntime(songs) {} +// const albumAverageRuntime = {}; +// let albumName = '' +// let longestAvgRuntime = 0 + +// songs.forEach(song => { +// if (!albumAverageRuntime[song.album]) { +// albumAverageRuntime[song.album] = { +// totalrunTime: song.runtimeInSeconds, +// songCount: 1, +// avgRuntime: runtimeInSeconds, +// }; +// } else { +// albumAverageRuntime[song.album].totalrunTime += song.runtimeInSeconds; +// albumAverageRuntime[song.album].songCount++; +// albumAverageRuntime[song.album] = avgRuntime +( +// albumAverageRuntime[song.album].totalrunTime / +// albumAverageRuntime[song.album].songCount +// ) +// } +// if (albumAverageRuntime[song.album].avgRuntime > longestAvgRuntime) { +// longestAvgRuntime = albumAverageRuntime[song.album].avgRuntime; +// albumName = song.album +// } +// }); +// +// return avgRuntime.albumName + +// } + // Problem #18 /** * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { +// let songsSortedByRuntime = songs.sort((songA,songB)=> songA.runtimeInSeconds - songB.runtimeInSeconds); + +// songsSortedByRuntime.forEach((song)=> console.log(song.title)) + +} // Problem #19 /** From c4739d85d0a40ad64b743716c5657b0d3b2f551d Mon Sep 17 00:00:00 2001 From: ViaticusRex Date: Mon, 15 Jan 2024 23:21:23 -0500 Subject: [PATCH 5/6] Just finished # 7 --- index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9fc66ee..d36b132 100644 --- a/index.js +++ b/index.js @@ -101,7 +101,11 @@ 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) { + if (songs.some(song => song.runtimeInSeconds > runtime)){ + return true + }; +} // #7 /** @@ -109,7 +113,14 @@ function isThereLongSong(songs, runtime) {} * @param {Object[]} songs - An array of songs. * @returns {Object[]} Array of song objects with runtime in minutes. */ -function getSongsWithDurationInMinutes(songs) {} + function getSongsWithDurationInMinutes(songs) { + return songs.map(song => { + return { + title: song.title, + durationInMinutes: song.runtimeInSeconds / 60 + }; + }); + } // #8 /** From eed40e8b964089cb255bbbd0d3d8629e09542973 Mon Sep 17 00:00:00 2001 From: ViaticusRex Date: Tue, 16 Jan 2024 01:03:17 -0500 Subject: [PATCH 6/6] Finished questions 9-11 --- index.js | 85 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index d36b132..1fdf998 100644 --- a/index.js +++ b/index.js @@ -60,25 +60,25 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { - // const albumWithMost = songs.reduce((acc,song) => { - // if (acc[song.album]) { - // acc[song.album]++ - // } else { - // acc[song.album] = 1 - // } - // return acc + const albumWithMost = songs.reduce((acc,song) => { + if (acc[song.album]) { + acc[song.album]++ + } else { + acc[song.album] = 1 + } + return acc - // }, {}); + }, {}); - // let count = 0 - // let result = '' - // for (const song in albumWithMost) { - // if(albumWithMost[song] > count) { - // count = albumWithMost[song]++ - // result = song.album - // } - // return result - // } + let count = 0 + let result = '' + for (const song in albumWithMost) { + if(albumWithMost[song] > count) { + count = albumWithMost[song]++ + result = song.album + } + return result + } } @@ -113,14 +113,12 @@ function isThereLongSong(songs, runtime) { * @param {Object[]} songs - An array of songs. * @returns {Object[]} Array of song objects with runtime in minutes. */ - function getSongsWithDurationInMinutes(songs) { - return songs.map(song => { - return { - title: song.title, - durationInMinutes: song.runtimeInSeconds / 60 - }; - }); - } +function getSongsWithDurationInMinutes(songs) { + return songs.map(song => ({ + title: song.title, + durationInMinutes: song.runtimeInSeconds / 60 + })); +} // #8 /** @@ -128,7 +126,17 @@ function isThereLongSong(songs, runtime) { * @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((b,a) => { + if (a>b) { + return -1; + } else if (b>a) { + return 1; + } else { + return 0; + } + }); +} // #9 /** @@ -137,7 +145,11 @@ 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) { + let listOfSongs = songs.filter(song => song.title.includes(word)); + let songTitles = listOfSongs.map(song => song.title); + return songTitles; +} // #10 /** @@ -146,14 +158,29 @@ 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) { + let songsByArtist = songs.filter(song => song.artist === artistName); + let runtimeCount = 0 + for (let i = 0; i < songsByArtist.length; i++) { + runtimeCount += songsByArtist[i].runtimeInSeconds + } + return runtimeCount +} // 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 artists = songs.map(song => song.artist); + + for (let i = 0; i < artists.length; i++) { + if (artists.indexOf(artists[i]) !== artists.lastIndexOf(artists[i])) { + console.log(artists[i]); + } + } +} // Problem #12 /**