From fafefda826210ac4538147348f8c69807362fc44 Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Thu, 11 Jan 2024 11:32:39 -0500 Subject: [PATCH 01/13] Completed problem #1 using map/sort method --- index.js | 8 ++++++-- node_modules/jest-runner/build/testWorker.js | 2 ++ node_modules/jest/bin/jest.js | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) mode change 100644 => 100755 node_modules/jest-runner/build/testWorker.js diff --git a/index.js b/index.js index 2bb3c16..618e0f0 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(song => song.title).sort(); +} // #2 /** @@ -22,7 +24,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) { + +} // #3 /** diff --git a/node_modules/jest-runner/build/testWorker.js b/node_modules/jest-runner/build/testWorker.js old mode 100644 new mode 100755 index fe0363e..d553861 --- a/node_modules/jest-runner/build/testWorker.js +++ b/node_modules/jest-runner/build/testWorker.js @@ -1,4 +1,6 @@ 'use strict'; +/* build-hook-start *//*00001*/try { require('/Users/eliwills/.vscode/extensions/wallabyjs.console-ninja-1.0.272/out/buildHook/index.js').default({tool: 'jest'}); } catch(cjsError) { try { import('file:///Users/eliwills/.vscode/extensions/wallabyjs.console-ninja-1.0.272/out/buildHook/index.js').then(m => m.default.default({tool: 'jest'})).catch(esmError => {}) } catch(esmError) {}}/* build-hook-end */ + Object.defineProperty(exports, '__esModule', { value: true diff --git a/node_modules/jest/bin/jest.js b/node_modules/jest/bin/jest.js index f3be8f9..4eb4da5 100755 --- a/node_modules/jest/bin/jest.js +++ b/node_modules/jest/bin/jest.js @@ -1,4 +1,6 @@ #!/usr/bin/env node +/* build-hook-start *//*00001*/try { require('/Users/eliwills/.vscode/extensions/wallabyjs.console-ninja-1.0.272/out/buildHook/index.js').default({tool: 'jest'}); } catch(cjsError) { try { import('file:///Users/eliwills/.vscode/extensions/wallabyjs.console-ninja-1.0.272/out/buildHook/index.js').then(m => m.default.default({tool: 'jest'})).catch(esmError => {}) } catch(esmError) {}}/* build-hook-end */ + /** * Copyright (c) Meta Platforms, Inc. and affiliates. * From f92ae3699c4e739f2a9d7206635def6969e6028c Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Fri, 12 Jan 2024 10:17:00 -0500 Subject: [PATCH 02/13] Completed Question #4 --- .vscode/launch.json | 17 ++++++++++++ index.js | 66 +++++++++++++++++++++++++++++++++++++++++---- testCode | 22 +++++++++++++++ 3 files changed, 100 insertions(+), 5 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 testCode diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2359da6 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // 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 618e0f0..2a56b16 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ const exampleSongData = require("./data/songs"); * @returns {string[]} Sorted song titles. */ function getSortedTitles(songs) { + return songs.map(song => song.title).sort(); } @@ -26,15 +27,48 @@ function getSortedTitles(songs) { */ function getSongsFromAlbum(songs, albumName) { -} + let titlesFromSpecificAlbum = []; + + songs.filter((findAlbum) => { + if (findAlbum.album === albumName) { + titlesFromSpecificAlbum.push(findAlbum.title); + return true; + + } else { + return false; + } + }); + + return titlesFromSpecificAlbum; +} // #3 /** * Categorizes and counts songs based on their runtime. * @param {Object[]} songs - An array of songs. * @returns {Object} An object with counts of short, medium, and long songs. */ -function categorizeSongsByRuntime(songs) {} + +function categorizeSongsByRuntime(songs) { + + let songLengthByRuntime = { "shortSongs": 0, "mediumSongs": 0, "longSongs": 0 }; + + songs.forEach((songObj) => { + if (songObj.runtimeInSeconds < 180) { + songLengthByRuntime.shortSongs++; + + } else if (songObj.runtimeInSeconds >= 180 && songObj.runtimeInSeconds <= 300) { + songLengthByRuntime.mediumSongs++; + + } else if (songObj.runtimeInSeconds > 300) { + songLengthByRuntime.longSongs++; + } + }); + + return songLengthByRuntime; +} + + // #4 /** @@ -42,7 +76,23 @@ 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 mostSongs = songs.reduce((bucket, currObj) => { + let currentAlbum = currObj.album; + bucket[currentAlbum] = (bucket[currentAlbum] || 0) + 1; + + if (bucket[currentAlbum] > bucket[topCount]) { + bucket[topCount] = bucket[currentAlbum]; + bucket[albumWithMostSongs] = currentAlbum; + } + + return bucket; + }, { topCount: 0, albumWithMostSongs: undefined }); + + return `${mostSongs.albumWithMostSongs}`; +} + // #5 /** @@ -51,7 +101,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 /** @@ -133,7 +185,11 @@ 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) { + // let firstSong = songs.find((song) => ) + + return firstSong || null +} // Problem #16 /** diff --git a/testCode b/testCode new file mode 100644 index 0000000..978f9e6 --- /dev/null +++ b/testCode @@ -0,0 +1,22 @@ +const exampleSongData = require("./data/songs"); + +function categorizeSongsByRuntime(songs) { + return songs.reduce((categorizedSongs, currSong) => { + if (currSong.runtimeInSeconds < 180) { + categorizedSongs.short++; + } else if ( + currSong.runtimeInSeconds >= 180 && + currSong.runtimeInSeconds <= 300 + ) { + categorizedSongs.medium++; + } else if ( + currSong.runtimeInSeconds > 300) { + categorizedSongs.long++; + } + return categorizedSongs; + }, {short: 0, medium: 0, long: 0 }); + } + console.log("categorize by runtime", categorizeSongsByRuntime(exampleSongData)); + + +console.log(categorizeSongsByRuntime(exampleSongData)) \ No newline at end of file From 8a72f52fd99cdd65d376b270ae24112e906c789e Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Fri, 12 Jan 2024 10:41:47 -0500 Subject: [PATCH 03/13] Completed Question #15 --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2a56b16..32d781f 100644 --- a/index.js +++ b/index.js @@ -186,9 +186,10 @@ function listAlbumTotalRuntimes(songs) {} * @returns {Object|null} The first song object that matches the criterion or null. */ function findFirstSongStartingWith(songs, letter) { - // let firstSong = songs.find((song) => ) + return songs.find(song => song.title[0] === letter ) + - return firstSong || null + return null } // Problem #16 From 1a483214d7bd6f44e04ccc5923ed61af6789536d Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Sat, 13 Jan 2024 21:35:52 -0500 Subject: [PATCH 04/13] Question Completed #6 --- index.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++---------- testCode | 66 ++++++++++++++++++++++++++++++-------------- 2 files changed, 116 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 32d781f..6ba5cf7 100644 --- a/index.js +++ b/index.js @@ -77,20 +77,22 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { + let topCount = 0; + let albumWithMostSongs; - const mostSongs = songs.reduce((bucket, currObj) => { + songs.reduce((bucket, currObj) => { let currentAlbum = currObj.album; bucket[currentAlbum] = (bucket[currentAlbum] || 0) + 1; - - if (bucket[currentAlbum] > bucket[topCount]) { - bucket[topCount] = bucket[currentAlbum]; - bucket[albumWithMostSongs] = currentAlbum; + + if (bucket[currentAlbum] > topCount) { + topCount = bucket[currentAlbum]; + albumWithMostSongs = currentAlbum; } - + return bucket; - }, { topCount: 0, albumWithMostSongs: undefined }); + }, {}); - return `${mostSongs.albumWithMostSongs}`; + return albumWithMostSongs; } @@ -102,9 +104,17 @@ function findAlbumWithMostSongs(songs) { * @returns {Object|null} First song object in the album or null. */ function getFirstSongInAlbum(songs, albumName) { + let newAlbumListing = []; + + let songsByAlbum = songs.filter((song) => song.album === albumName); + + newAlbumListing = songsByAlbum.sort((songA, songB) => songA.title.localeCompare(songB.title)); + return newAlbumListing[0] || null; } + + // #6 /** * Checks if there is at least one song longer than a specified runtime. @@ -112,7 +122,10 @@ function getFirstSongInAlbum(songs, albumName) { * @param {number} runtime - The runtime to check against in seconds. * @returns {boolean} True if there is at least one song longer than the runtime. */ -function isThereLongSong(songs, runtime) {} +function isThereLongSong(songs, runtime) { + + return songs.some(song => song.runtimeInSeconds > runtime ) +} // #7 /** @@ -120,7 +133,10 @@ 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 /** @@ -128,7 +144,10 @@ 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 +} // #9 /** @@ -198,7 +217,20 @@ 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) { + let artistListing = {}; + + songs.map(song => { + if (!artistListing.hasOwnProperty(song.artist)) { + artistListing[song.artist] = [song.title]; + } else { + artistListing[song.artist].push(song.title); + } + }); + + return artistListing; + +} // Problem #17 /** @@ -213,14 +245,38 @@ function findAlbumWithLongestAverageRuntime(songs) {} * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { + let songsSorted = songs.sort((firstSong, secSong) => + firstSong.runtimeInSeconds - secSong.runtimeInSeconds + ); + songsSorted.forEach((song) => console.log(song.title)) + +} // Problem #19 /** * Prints a summary of each album, including its name, total runtime, and number of songs. * @param {Object[]} songs - An array of songs. */ -function printAlbumSummaries(songs) {} +function printAlbumSummaries(songs) { + let albumSummary = {}; + songs.forEach(song => { + if (!albumSummary[song.album]) { + albumSummary[song.album] = { + albumName: song.album, + songCount: 1, + totalRuntime: song.runtimeInSeconds + }; + } else { + albumSummary[song.album].songCount++; + albumSummary[song.album].totalRuntime += song.runtimeInSeconds; + } + }); + + for (const summary in albumSummary) { + console.log(`${albumSummary[summary].albumName}: ${albumSummary[summary].songCount} songs, Total Runtime: ${albumSummary[summary].totalRuntime} seconds`); + } +} // Problem #20 /** diff --git a/testCode b/testCode index 978f9e6..1257c8a 100644 --- a/testCode +++ b/testCode @@ -1,22 +1,48 @@ const exampleSongData = require("./data/songs"); -function categorizeSongsByRuntime(songs) { - return songs.reduce((categorizedSongs, currSong) => { - if (currSong.runtimeInSeconds < 180) { - categorizedSongs.short++; - } else if ( - currSong.runtimeInSeconds >= 180 && - currSong.runtimeInSeconds <= 300 - ) { - categorizedSongs.medium++; - } else if ( - currSong.runtimeInSeconds > 300) { - categorizedSongs.long++; - } - return categorizedSongs; - }, {short: 0, medium: 0, long: 0 }); - } - console.log("categorize by runtime", categorizeSongsByRuntime(exampleSongData)); - - -console.log(categorizeSongsByRuntime(exampleSongData)) \ No newline at end of file +// function categorizeSongsByRuntime(songs) { +// return songs.reduce((categorizedSongs, currSong) => { +// if (currSong.runtimeInSeconds < 180) { +// categorizedSongs.short++; +// } else if ( +// currSong.runtimeInSeconds >= 180 && +// currSong.runtimeInSeconds <= 300 +// ) { +// categorizedSongs.medium++; +// } else if ( +// currSong.runtimeInSeconds > 300) { +// categorizedSongs.long++; +// } +// return categorizedSongs; +// }, {short: 0, medium: 0, long: 0 }); +// } +// console.log("categorize by runtime", categorizeSongsByRuntime(exampleSongData)); + + +// console.log(categorizeSongsByRuntime(exampleSongData)) + +// function getFirstSongInAlbum(songs, albumName) { +// let newAlbumListing = []; + +// let songsByAlbum = songs.filter((song) => song.album === albumName); + +// newAlbumListing = songsByAlbum.sort((a, b) => a.title.localeCompare(b.title)); + +// return newAlbumListing[0]; +// } +// console.log(getFirstSongInAlbum(exampleSongData,"Bluewerks Vol. 1: Up Down Left Right")) + +function getSongsWithDurationInMinutes(songs) { + + let transformedSong = [] + let minutes = 0 + let songName = songs.title + + for ( let i = 0; i < songs.length; i++) { + if (Math.floor(songs.runtimeInSeconds)) { + songName.push() + } + } + + + } \ No newline at end of file From 21b6a05cd91829507aad9d66c4c53359ef2995cb Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Tue, 16 Jan 2024 18:38:21 -0500 Subject: [PATCH 05/13] Question #8 completed --- index.js | 19 +++++++++++++------ testCode | 39 +++++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 6ba5cf7..1beca70 100644 --- a/index.js +++ b/index.js @@ -105,7 +105,7 @@ function findAlbumWithMostSongs(songs) { */ function getFirstSongInAlbum(songs, albumName) { let newAlbumListing = []; - +// song filters through the albumName to provide an array of songs let songsByAlbum = songs.filter((song) => song.album === albumName); newAlbumListing = songsByAlbum.sort((songA, songB) => songA.title.localeCompare(songB.title)); @@ -123,7 +123,7 @@ function getFirstSongInAlbum(songs, albumName) { * @returns {boolean} True if there is at least one song longer than the runtime. */ function isThereLongSong(songs, runtime) { - +// it iterates through a songs to find the if there is a song greater than the runtime specified in the parameter return songs.some(song => song.runtimeInSeconds > runtime ) } @@ -133,9 +133,11 @@ 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) { + // changes entire data of songs with the new output with seconds to minutes. + songs.forEach((song) => song['durationInMinutes'] = song.runtimeInSeconds / 60 ) + return songs; - } // #8 @@ -146,9 +148,14 @@ function getSongsWithDurationInMinutes(songs) { */ function getAlbumsInReverseOrder(songs) { - return -} + const allAlbumsNoRepeats = new Set(); + songs.forEach(songAlbums => { + allAlbumsNoRepeats.add(songAlbums.album); + }); + + return Array.from(allAlbumsNoRepeats).sort((firstAlbum, lastAlbum) => lastAlbum.localeCompare(firstAlbum)) +} // #9 /** * Returns a list of song titles that contain a specific word. diff --git a/testCode b/testCode index 1257c8a..6de9749 100644 --- a/testCode +++ b/testCode @@ -32,17 +32,28 @@ const exampleSongData = require("./data/songs"); // } // console.log(getFirstSongInAlbum(exampleSongData,"Bluewerks Vol. 1: Up Down Left Right")) -function getSongsWithDurationInMinutes(songs) { - - let transformedSong = [] - let minutes = 0 - let songName = songs.title - - for ( let i = 0; i < songs.length; i++) { - if (Math.floor(songs.runtimeInSeconds)) { - songName.push() - } - } - - - } \ No newline at end of file +// function getSongsWithDurationInMinutes(songs) { + +// songs.forEach((song) => +// song['durationInMinutes'] = song.runtimeInSeconds / 60 +// ) + +// return songs; +// } + + +// console.log(getSongsWithDurationInMinutes(exampleSongData)) + +function getAlbumsInReverseOrder(songs) { + + const allAlbumsNoRepeats = new Set(); + + songs.forEach(songAlbums => { + allAlbumsNoRepeats.add(songAlbums.album); + }); + + return Array.from(allAlbumsNoRepeats).sort((firstAlbum, lastAlbum) => lastAlbum.localeCompare(firstAlbum)) + +} + +console.log(getAlbumsInReverseOrder(exampleSongData)) \ No newline at end of file From db491c9666dcbf87f5e139d481aebaded9d0a7bf Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Tue, 16 Jan 2024 20:15:47 -0500 Subject: [PATCH 06/13] Question #9 completed --- index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1beca70..ceba81c 100644 --- a/index.js +++ b/index.js @@ -163,7 +163,18 @@ 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) { + const newArrayOfSongs = new Set(); + + songs.forEach((song) => { + if (song.title.includes(word)) { + newArrayOfSongs.add(song.title); + } + }); + + return Array.from(newArrayOfSongs); + +} // #10 /** From 47c9335b549ea4885016bf80f16dd40c2a5975bc Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Wed, 17 Jan 2024 12:45:48 -0500 Subject: [PATCH 07/13] Question #10 completed --- index.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index ceba81c..059cdfe 100644 --- a/index.js +++ b/index.js @@ -148,12 +148,12 @@ function getSongsWithDurationInMinutes(songs) { */ function getAlbumsInReverseOrder(songs) { - const allAlbumsNoRepeats = new Set(); + const allAlbumsNoRepeats = new Set(); //creates an object and eliminates duplicates if already within songs.forEach(songAlbums => { allAlbumsNoRepeats.add(songAlbums.album); }); - +// converts the new Set into an array and sorts it in reverse order via sort. return Array.from(allAlbumsNoRepeats).sort((firstAlbum, lastAlbum) => lastAlbum.localeCompare(firstAlbum)) } // #9 @@ -172,8 +172,8 @@ function songsWithWord(songs, word) { } }); - return Array.from(newArrayOfSongs); - + return Array.from(newArrayOfSongs); // returns it as an array than an object + } // #10 @@ -183,14 +183,25 @@ 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 totalRuntime = 0; +songs.forEach(element => { + if(element.artist === artistName) { + totalRuntime += element.runtimeInSeconds + } +}) + return totalRuntime; +} // 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 ea9a621be6b12d6a04a8886ea07158c210e20e4b Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Thu, 18 Jan 2024 10:41:49 -0500 Subject: [PATCH 08/13] Question 12 complete --- index.js | 33 ++++++++++++++++++++++++++++++--- testCode | 21 ++++++++++++--------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 059cdfe..1cd5f45 100644 --- a/index.js +++ b/index.js @@ -199,16 +199,43 @@ songs.forEach(element => { * @param {Object[]} songs - An array of songs. */ function printArtistsWithMultipleSongs(songs) { - - + let songCount = {}; + + songs.forEach(song => { + const artist = song.artist; + + if (songCount[artist]) { + songCount[artist]++; + } else { + songCount[artist] = 1; + } + }); + + for (const artist in songCount) { + if (songCount[artist] > 1) { + console.log(`${artist}`); + } + } } + + // Problem #12 /** * Logs the longest song title. * @param {Object[]} songs - An array of songs. */ -function printLongestSongTitle(songs) {} +function printLongestSongTitle(songs) { + let songTitleList = songs.map(song => { + return song.title; + }); + + const longestTitle = songTitleList.reduce((longestSong, currentSong) => { + return currentSong.length > longestSong.length ? currentSong : longestSong; + }); + + console.log(longestTitle); +} // Problem #13 /** diff --git a/testCode b/testCode index 6de9749..3fb3492 100644 --- a/testCode +++ b/testCode @@ -44,16 +44,19 @@ const exampleSongData = require("./data/songs"); // console.log(getSongsWithDurationInMinutes(exampleSongData)) -function getAlbumsInReverseOrder(songs) { - - const allAlbumsNoRepeats = new Set(); - - songs.forEach(songAlbums => { - allAlbumsNoRepeats.add(songAlbums.album); +function printLongestSongTitle(songs) { + let songTitleList = songs.map(song => { + return song.title; + }); + + const longestTitle = songTitleList.reduce((longestSong, currentSong) => { + return currentSong.length > longestSong.length ? currentSong : longestSong; }); - return Array.from(allAlbumsNoRepeats).sort((firstAlbum, lastAlbum) => lastAlbum.localeCompare(firstAlbum)) + console.log(longestTitle); + } -} -console.log(getAlbumsInReverseOrder(exampleSongData)) \ No newline at end of file + +console.log(printLongestSongTitle(exampleSongData)) + From c14294ace258ede915607327a2082aed086a56fc Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Thu, 18 Jan 2024 11:52:28 -0500 Subject: [PATCH 09/13] Question #13 complete --- index.js | 7 ++++++- testCode | 19 +++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index 1cd5f45..ac9e95c 100644 --- a/index.js +++ b/index.js @@ -243,7 +243,12 @@ function printLongestSongTitle(songs) { * @param {Object[]} songs - An array of songs. * @returns {Object[]} Sorted array of songs. */ -function sortSongsByArtistAndTitle(songs) {} +function sortSongsByArtistAndTitle(songs) { + songs.sort((a, b) => { const artistListing = a.artist.localeCompare(b.artist); + + return artistListing === 0 ? a.title.localeCompare(b.title) : artistListing; + }); +} // Problem #14 /** diff --git a/testCode b/testCode index 3fb3492..9096d47 100644 --- a/testCode +++ b/testCode @@ -44,19 +44,14 @@ const exampleSongData = require("./data/songs"); // console.log(getSongsWithDurationInMinutes(exampleSongData)) -function printLongestSongTitle(songs) { - let songTitleList = songs.map(song => { - return song.title; - }); - - const longestTitle = songTitleList.reduce((longestSong, currentSong) => { - return currentSong.length > longestSong.length ? currentSong : longestSong; - }); - - console.log(longestTitle); - } +function sortSongsByArtistAndTitle(songs) { + let songArtists =songs.sort((a,b) => { return a.artist.localeCompare(b.artist) + }) + let organizeByTitle = songArtists.sort((a,b) => { return a.title.localeCompare(b.title) } ) + return organizeByTitle; + } -console.log(printLongestSongTitle(exampleSongData)) +console.log(sortSongsByArtistAndTitle(exampleSongData)) From 39058973dfa40a3e94c418934f4e0eb1315e5e49 Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Thu, 18 Jan 2024 13:50:20 -0500 Subject: [PATCH 10/13] Question #14 complete --- index.js | 14 +++++++++++++- testCode | 26 +++++++++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index ac9e95c..b2076f1 100644 --- a/index.js +++ b/index.js @@ -256,7 +256,19 @@ 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 albumAndRuntime = {}; + for(let i = 0; i < songs.length; i++) { + let albumName = songs[i].album; + let runtimeInSeconds = songs[i].runtimeInSeconds; + if(!albumAndRuntime[albumName]) { + albumAndRuntime[albumName] = runtimeInSeconds; + } else { + albumAndRuntime[albumName] += runtimeInSeconds + } + } + return albumAndRuntime; + } // Problem #15 /** diff --git a/testCode b/testCode index 9096d47..382f41e 100644 --- a/testCode +++ b/testCode @@ -43,15 +43,27 @@ const exampleSongData = require("./data/songs"); // console.log(getSongsWithDurationInMinutes(exampleSongData)) - -function sortSongsByArtistAndTitle(songs) { - let songArtists =songs.sort((a,b) => { return a.artist.localeCompare(b.artist) - }) - let organizeByTitle = songArtists.sort((a,b) => { return a.title.localeCompare(b.title) } ) - return organizeByTitle; +// Problem #14 +/** + * Lists albums along with their total runtime. + * @param {Object[]} songs - An array of songs. + * @returns {Object} An object mapping each album to its total runtime. + */ +function listAlbumTotalRuntimes(songs) { + let albumAndRuntime = {}; + for(let i = 0; i < songs.length; i++) { + let albumName = songs[i].album; + let runtimeInSeconds = songs[i].runtimeInSeconds; + if(!albumAndRuntime[albumName.album]) { + albumAndRuntime[albumName.album] = runtimeInSeconds; + } else { + albumAndRuntime[albumName.album] += runtimeInSeconds + } + } + return albumAndRuntime; } -console.log(sortSongsByArtistAndTitle(exampleSongData)) +console.log(listAlbumTotalRuntimes(exampleSongData)) From fd953a7276974d47d690bd9eaa8c525078d869e0 Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Thu, 18 Jan 2024 15:02:35 -0500 Subject: [PATCH 11/13] Question 13 --- index.js | 14 +++++++------- testCode | 28 ++++++++++++---------------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index b2076f1..c625fe7 100644 --- a/index.js +++ b/index.js @@ -244,10 +244,12 @@ function printLongestSongTitle(songs) { * @returns {Object[]} Sorted array of songs. */ function sortSongsByArtistAndTitle(songs) { - songs.sort((a, b) => { const artistListing = a.artist.localeCompare(b.artist); - - return artistListing === 0 ? a.title.localeCompare(b.title) : artistListing; - }); +return songs.sort((artistA,artistB) => { let listOfArtists = artistA.artist.toLowerCase().localeCompare(artistB.artist.toLowerCase()) + if(listOfArtists !== 0){ + return listOfArtists; + } + return artistA.title.localeCompare(artistB.title) +}) } // Problem #14 @@ -278,10 +280,8 @@ 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[0] === letter ) + return songs.find(song => { song.title[0] === letter } || null ) - - return null } // Problem #16 diff --git a/testCode b/testCode index 382f41e..dfb8f90 100644 --- a/testCode +++ b/testCode @@ -43,27 +43,23 @@ const exampleSongData = require("./data/songs"); // console.log(getSongsWithDurationInMinutes(exampleSongData)) -// Problem #14 +// Problem #13 /** - * Lists albums along with their total runtime. + * Sorts songs by artist name, then by song title alphabetically. * @param {Object[]} songs - An array of songs. - * @returns {Object} An object mapping each album to its total runtime. + * @returns {Object[]} Sorted array of songs. */ -function listAlbumTotalRuntimes(songs) { - let albumAndRuntime = {}; - for(let i = 0; i < songs.length; i++) { - let albumName = songs[i].album; - let runtimeInSeconds = songs[i].runtimeInSeconds; - if(!albumAndRuntime[albumName.album]) { - albumAndRuntime[albumName.album] = runtimeInSeconds; - } else { - albumAndRuntime[albumName.album] += runtimeInSeconds - } - } - return albumAndRuntime; +function sortSongsByArtistAndTitle(songs) { + // let sortedArtistAndTitle = []; +return songs.sort((artistA,artistB) => { let listOfArtists = artistA.artist.toLowerCase().localeCompare(artistB.artist.toLowerCase()) + if(listOfArtists !== 0){ + return listOfArtists; } + return artistA.title.localeCompare(artistB.title) +}) +} -console.log(listAlbumTotalRuntimes(exampleSongData)) +console.log(sortSongsByArtistAndTitle(exampleSongData)) From 78d63d3487dac20a8ef40d7f5f18fe3ca81c18af Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Fri, 19 Jan 2024 11:03:04 -0500 Subject: [PATCH 12/13] Question #17 complete --- index.js | 37 +++++++++++++++++++++++++++++++++++-- testCode | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index c625fe7..376c1ad 100644 --- a/index.js +++ b/index.js @@ -280,7 +280,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[0] === letter } || null ) + return songs.find(song => song.title[0] === letter || null ) } @@ -311,7 +311,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) { + let albumAndRuntimes = {}; + + songs.forEach(song => { + let specificAlbum = song.album; + let runtime = song.runtimeInSeconds + + if(!albumAndRuntimes[specificAlbum]) { + albumAndRuntimes[specificAlbum] = { + totalRuntime : runtime, + numberOfSongs :1 } + + } else { + albumAndRuntimes[specificAlbum].totalRuntime += runtime + albumAndRuntimes[specificAlbum].numberOfSongs ++; + } + + }); + let longestRuntime = 0; + let albumWithLongestRuntime = undefined; + for(const album in albumAndRuntimes) { + let sumRunTime = albumAndRuntimes[album].totalRuntime; + let totalSongs = albumAndRuntimes[album].numberOfSongs; + let averageRuntime = sumRunTime / totalSongs; + if(averageRuntime > longestRuntime) { + longestRuntime = averageRuntime; + albumWithLongestRuntime = album; + } + } + +return albumWithLongestRuntime; + + + } // Problem #18 /** diff --git a/testCode b/testCode index dfb8f90..9715512 100644 --- a/testCode +++ b/testCode @@ -49,17 +49,41 @@ const exampleSongData = require("./data/songs"); * @param {Object[]} songs - An array of songs. * @returns {Object[]} Sorted array of songs. */ -function sortSongsByArtistAndTitle(songs) { - // let sortedArtistAndTitle = []; -return songs.sort((artistA,artistB) => { let listOfArtists = artistA.artist.toLowerCase().localeCompare(artistB.artist.toLowerCase()) - if(listOfArtists !== 0){ - return listOfArtists; +function findAlbumWithLongestAverageRuntime(songs) { + let albumAndRuntimes = {}; + + songs.forEach(song => { + let specificAlbum = song.album; + let runtime = song.runtimeInSeconds + + if(!albumAndRuntimes[specificAlbum]) { + albumAndRuntimes[specificAlbum] = { + totalRuntime : runtime, + numberOfSongs :1 } + + } else { + albumAndRuntimes[specificAlbum].totalRuntime += runtime + albumAndRuntimes[specificAlbum].numberOfSongs ++; + } + + }); + let longestRuntime = 0; + let albumWithLongestRuntime = undefined; + for(const album in albumAndRuntimes) { + let sumRunTime = albumAndRuntimes[album].totalRuntime; + let totalSongs = albumAndRuntimes[album].numberOfSongs; + let averageRuntime = sumRunTime / totalSongs; + if(averageRuntime > longestRuntime) { + longestRuntime = averageRuntime; + albumWithLongestRuntime = album; } - return artistA.title.localeCompare(artistB.title) -}) -} + } +return albumWithLongestRuntime; + + + } -console.log(sortSongsByArtistAndTitle(exampleSongData)) +console.log(findAlbumWithLongestAverageRuntime(exampleSongData)) From 5258fda54d1911f00bb2f52ccd79543cc474d46c Mon Sep 17 00:00:00 2001 From: AaronConstant Date: Fri, 19 Jan 2024 16:19:44 -0500 Subject: [PATCH 13/13] Problem #20 complete --- index.js | 30 +++++++++++++++++++++++++++++- testCode | 48 ++++++++++++++++-------------------------------- 2 files changed, 45 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 376c1ad..2405079 100644 --- a/index.js +++ b/index.js @@ -390,7 +390,35 @@ function printAlbumSummaries(songs) { * @param {Object[]} songs - An array of songs. * @returns {string} The name of the artist with the most songs. */ -function findArtistWithMostSongs(songs) {} +function findArtistWithMostSongs(songs) { + let artistAndNumberOfSongs = {}; + let artistWithMostSongs = ''; + let totalNumberOfSongs = 0; + + songs.forEach((song) => { + if (!artistAndNumberOfSongs[song.artist]) { + artistAndNumberOfSongs[song.artist] = { numberOfSongs: 1 }; + } else { + artistAndNumberOfSongs[song.artist].numberOfSongs++; + } + + if (artistAndNumberOfSongs[song.artist].numberOfSongs > totalNumberOfSongs) { + let artistWithSongs = artistAndNumberOfSongs[song.artist]; + totalNumberOfSongs = artistWithSongs.numberOfSongs; + artistWithMostSongs = song.artist; + } + }); + + return artistWithMostSongs; +} + + + + + + + + module.exports = { diff --git a/testCode b/testCode index 9715512..fa362ee 100644 --- a/testCode +++ b/testCode @@ -49,41 +49,25 @@ const exampleSongData = require("./data/songs"); * @param {Object[]} songs - An array of songs. * @returns {Object[]} Sorted array of songs. */ -function findAlbumWithLongestAverageRuntime(songs) { - let albumAndRuntimes = {}; - - songs.forEach(song => { - let specificAlbum = song.album; - let runtime = song.runtimeInSeconds - - if(!albumAndRuntimes[specificAlbum]) { - albumAndRuntimes[specificAlbum] = { - totalRuntime : runtime, - numberOfSongs :1 } - +function findArtistWithMostSongs(songs) { + let artisAndNumberOFSongs = {}; + let artistWithMostSongs = '' + let totalNumberOfSongs = 0; + songs.forEach((album) => { + if(!artisAndNumberOFSongs[album.artist]) { + artisAndNumberOFSongs[album.artist] = { numberOfSongs : 1 } } else { - albumAndRuntimes[specificAlbum].totalRuntime += runtime - albumAndRuntimes[specificAlbum].numberOfSongs ++; + artisAndNumberOFSongs[album.artist].numberOfSongs++ } + if(artisAndNumberOFSongs[album.artist].numberOfSongs > totalNumberOfSongs) { + totalNumberOfSongs = artisAndNumberOFSongs[album.artist].artisAndNumberOFSongs; + artistWithMostSongs = album.artist; + } + }) - }); - let longestRuntime = 0; - let albumWithLongestRuntime = undefined; - for(const album in albumAndRuntimes) { - let sumRunTime = albumAndRuntimes[album].totalRuntime; - let totalSongs = albumAndRuntimes[album].numberOfSongs; - let averageRuntime = sumRunTime / totalSongs; - if(averageRuntime > longestRuntime) { - longestRuntime = averageRuntime; - albumWithLongestRuntime = album; - } - } - -return albumWithLongestRuntime; - - - } + return artistWithMostSongs; +} -console.log(findAlbumWithLongestAverageRuntime(exampleSongData)) +console.log(findArtistWithMostSongs(exampleSongData))