From b68c3f63d63848493647d44cebfbfb8988708ecd Mon Sep 17 00:00:00 2001 From: ExiledSPKhy Date: Thu, 11 Jan 2024 11:20:23 -0500 Subject: [PATCH 1/3] Update problems solved: 1, 2, 3 --- index.js | 129 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 88 insertions(+), 41 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..5ca78fd 100644 --- a/index.js +++ b/index.js @@ -3,34 +3,67 @@ Native Array Methods pt.2 continues with the same dataset: songs. All required f */ +const songs = require("./data/songs"); const exampleSongData = require("./data/songs"); // Do not change the line above. // #1 /** - * Returns the titles of songs sorted alphabetically. - * @param {Object[]} songs - An array of songs. - * @returns {string[]} Sorted song titles. - */ -function getSortedTitles(songs) {} - -// #2 -/** - * Returns the titles of all songs from a specified album. - * @param {Object[]} songs - An array of songs. - * @param {string} albumName - Name of the album. - * @returns {string[]} An array of song titles. - */ -function getSongsFromAlbum(songs, albumName) {} - +// * Returns the titles of songs sorted alphabetically. +// * @param {Object[]} songs - An array of songs. +// * @returns {string[]} Sorted song titles. +// */ +function getSortedTitles(songs) { + return songs.map(x => x.title).sort() + } +///console.log(getSortedTitles(exampleSongData)); + +// // #2 +// /** +// * Returns the titles of all songs from a specified album. +// * @param {Object[]} songs - An array of songs. +// * @param {string} albumName - Name of the album. +// * @returns {string[]} An array of song titles. +// */ +function getSongsFromAlbum(songs, albumName) { + let newA =[ ] + for(let k=0; k < songs.length; k++){ + if(songs[k].album === albumName){ + newA.push( songs[k].title); + } + } + return newA; +} +// console.log(getSongsFromAlbum(exampleSongData,"Bluewerks Vol. 1: Up Down Left Right")); // #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 shortSongs = 0; + let mediumSongs = 0; + let longSongs = 0; + + for (let i = 0; i < songs.length; i++) { + if (songs[i].runtimeInSeconds < 180) { + shortSongs++; + } else if (songs[i].runtimeInSeconds >= 180 && songs[i].runtimeInSeconds <= 300) { + mediumSongs++; + } else if (songs[i].runtimeInSeconds > 300) { + longSongs++; + } + } + + const songObj = { shortSongs, mediumSongs, longSongs }; + return songObj; +} + + + // #4 /** @@ -38,8 +71,13 @@ 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 albumNames = songs.map((songAlbum)=> songAlbum.album) + console.log(albumNames); + } + findAlbumWithMostSongs(exampleSongData); + + // #5 /** * Returns details of the first song in a specific album. @@ -47,7 +85,16 @@ 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) { + + + // findIndex(callback(element, index, array)): Returns the index of the + // first element in the array that satisfies the provided testing function. + + + return + //returns an object with value of the first in an album or null +} // #6 /** @@ -170,25 +217,25 @@ function printAlbumSummaries(songs) {} function findArtistWithMostSongs(songs) {} -module.exports = { - getSortedTitles, - getSongsFromAlbum, - categorizeSongsByRuntime, - findAlbumWithMostSongs, - getFirstSongInAlbum, - isThereLongSong, - getSongsWithDurationInMinutes, - getAlbumsInReverseOrder, - songsWithWord, - getTotalRuntimeOfArtist, - printArtistsWithMultipleSongs, - sortSongsByArtistAndTitle, - printLongestSongTitle, - listAlbumTotalRuntimes, - findFirstSongStartingWith, - mapArtistsToSongs, - findAlbumWithLongestAverageRuntime, - printSongsSortedByRuntime, - printAlbumSummaries, - findArtistWithMostSongs -};; + module.exports = { + getSortedTitles, + getSongsFromAlbum, + categorizeSongsByRuntime, + findAlbumWithMostSongs, + getFirstSongInAlbum, + isThereLongSong, + getSongsWithDurationInMinutes, + getAlbumsInReverseOrder, + songsWithWord, + getTotalRuntimeOfArtist, + printArtistsWithMultipleSongs, + sortSongsByArtistAndTitle, + printLongestSongTitle, + listAlbumTotalRuntimes, + findFirstSongStartingWith, + mapArtistsToSongs, + findAlbumWithLongestAverageRuntime, + printSongsSortedByRuntime, + printAlbumSummaries, + findArtistWithMostSongs + };; From c54631d0da37b6e63aa93b0303f2fc2af4cf707c Mon Sep 17 00:00:00 2001 From: ExiledSPKhy Date: Fri, 12 Jan 2024 13:33:15 -0500 Subject: [PATCH 2/3] refactored problem 5, attempting to make terminal work --- index.js | 77 ++++++++++---- index.js.BAK | 277 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 334 insertions(+), 20 deletions(-) create mode 100644 index.js.BAK diff --git a/index.js b/index.js index 5ca78fd..e96254d 100644 --- a/index.js +++ b/index.js @@ -44,26 +44,28 @@ function getSongsFromAlbum(songs, albumName) { */ function categorizeSongsByRuntime(songs) { - let shortSongs = 0; - let mediumSongs = 0; - let longSongs = 0; + let songObj = { + shortSongs: 0, + mediumSongs: 0, + longSongs: 0, + } for (let i = 0; i < songs.length; i++) { if (songs[i].runtimeInSeconds < 180) { - shortSongs++; + songObj.shortSongs++; } else if (songs[i].runtimeInSeconds >= 180 && songs[i].runtimeInSeconds <= 300) { - mediumSongs++; + songObj.mediumSongs++; } else if (songs[i].runtimeInSeconds > 300) { - longSongs++; + songObj.longSongs++; } + + } + return songObj; } - - const songObj = { shortSongs, mediumSongs, longSongs }; - return songObj; -} - +console.log(categorizeSongsByRuntime(exampleSongData)); + // #4 /** @@ -72,12 +74,34 @@ function categorizeSongsByRuntime(songs) { * @returns {string} The name of the album with the most songs. */ function findAlbumWithMostSongs(songs) { - let albumNames = songs.map((songAlbum)=> songAlbum.album) - console.log(albumNames); + const albumSongCount = {}; + + // Iterate through songs to count the songs for each album + songs.forEach(song => { + const albumName = song.album;//aka Album name + + if (albumSongCount[albumName]) { + albumSongCount[albumName]++; + } else { + albumSongCount[albumName] = 1; + } + }); + + let maxSongs = 0; + let albumWithMostSongs; + + // Iterate through albumSongCount to find the album with the most songs + for (const album in albumSongCount) { + if (albumSongCount[album] > maxSongs) { + maxSongs = albumSongCount[album]; + albumWithMostSongs = album; + } } - findAlbumWithMostSongs(exampleSongData); - - + + return albumWithMostSongs; +} +console.log(findAlbumWithMostSongs(exampleSongData)) + // #5 /** * Returns details of the first song in a specific album. @@ -86,15 +110,20 @@ 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)|| null + + +} + getFirstSongInAlbum(exampleSongData); // findIndex(callback(element, index, array)): Returns the index of the // first element in the array that satisfies the provided testing function. - return + // return //returns an object with value of the first in an album or null -} +// } // #6 /** @@ -199,13 +228,21 @@ function findAlbumWithLongestAverageRuntime(songs) {} * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { // Problem #19 /** * Prints a summary of each album, including its name, total runtime, and number of songs. * @param {Object[]} songs - An array of songs. - */ + */let albumSummaries = {} + songs.forEach(song => { + if(!albumSummaries[song.album]){ + albumSummaries[song.album] ={ albumName: song.album, songCount: 1, totalRuntime: song.runtimeInSeconds}; + } + + }) + } + function printAlbumSummaries(songs) {} // Problem #20 diff --git a/index.js.BAK b/index.js.BAK new file mode 100644 index 0000000..ad1d831 --- /dev/null +++ b/index.js.BAK @@ -0,0 +1,277 @@ +/* +Native Array Methods pt.2 continues with the same dataset: songs. All required functions and array methods (forEach, map, find, some/every, sort) are combined into a single file, each addressing a distinct problem. +*/ + + +const songs = require("./data/songs"); +const exampleSongData = require("./data/songs"); +// Do not change the line above. + + +// #1 +/** +// * Returns the titles of songs sorted alphabetically. +// * @param {Object[]} songs - An array of songs. +// * @returns {string[]} Sorted song titles. +// */ +function getSortedTitles(songs) { + return songs.map(x => x.title).sort() + } +///console.log(getSortedTitles(exampleSongData)); + +// // #2 +// /** +// * Returns the titles of all songs from a specified album. +// * @param {Object[]} songs - An array of songs. +// * @param {string} albumName - Name of the album. +// * @returns {string[]} An array of song titles. +// */ +function getSongsFromAlbum(songs, albumName) { + let newA =[ ] + for(let k=0; k < songs.length; k++){ + if(songs[k].album === albumName){ + newA.push( songs[k].title); + } + } + return newA; +} +// console.log(getSongsFromAlbum(exampleSongData,"Bluewerks Vol. 1: Up Down Left Right")); +// #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) { + let songObj = { + + shortSongs: 0, + mediumSongs: 0, + longSongs: 0, + } + for (let i = 0; i < songs.length; i++) { + if (songs[i].runtimeInSeconds < 180) { + songObj.shortSongs++; + } else if (songs[i].runtimeInSeconds >= 180 && songs[i].runtimeInSeconds <= 300) { + songObj.mediumSongs++; + } else if (songs[i].runtimeInSeconds > 300) { + songObj.longSongs++; + } + + } + return songObj; + } +console.log(categorizeSongsByRuntime(exampleSongData)); + + + + +// #4 +/** + * Finds the album with the highest number of songs. + * @param {Object[]} songs - An array of songs. + * @returns {string} The name of the album with the most songs. + */ +function findAlbumWithMostSongs(songs) { + const albumSongCount = {}; + + // Iterate through songs to count the songs for each album + songs.forEach(song => { + const albumName = song.album;//aka Album name + + if (albumSongCount[albumName]) { + albumSongCount[albumName]++; + } else { + albumSongCount[albumName] = 1; + } + }); + + let maxSongs = 0; + let albumWithMostSongs; + + // Iterate through albumSongCount to find the album with the most songs + for (const album in albumSongCount) { + if (albumSongCount[album] > maxSongs) { + maxSongs = albumSongCount[album]; + albumWithMostSongs = album; + } + } + + return albumWithMostSongs; +} +console.log(findAlbumWithMostSongs(exampleSongData)) + +// #5 +/** + * Returns details of the first song in a specific album. + * @param {Object[]} songs - An array of songs. + * @param {string} albumName - Name of the album. + * @returns {Object|null} First song object in the album or null. + */ +function getFirstSongInAlbum(songs, albumName) { + return songs.find(song => song.album === albumName)|| null + + +} + getFirstSongInAlbum(exampleSongData); + + + // findIndex(callback(element, index, array)): Returns the index of the + // first element in the array that satisfies the provided testing function. + + + // return + //returns an object with value of the first in an album or null +// } + +// #6 +/** + * Checks if there is at least one song longer than a specified runtime. + * @param {Object[]} songs - An array of songs. + * @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) {} + +// #7 +/** + * Transforms song data to show title and runtime in minutes. + * @param {Object[]} songs - An array of songs. + * @returns {Object[]} Array of song objects with runtime in minutes. + */ +function getSongsWithDurationInMinutes(songs) {} + +// #8 +/** + * Returns the album names in reverse alphabetical order. + * @param {Object[]} songs - An array of songs. + * @returns {string[]} Array of album names in reverse alphabetical order. + */ +function getAlbumsInReverseOrder(songs) {} + +// #9 +/** + * Returns a list of song titles that contain a specific word. + * @param {Object[]} songs - An array of 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) {} + +// #10 +/** + * Returns the total runtime of songs by a specific artist. + * @param {Object[]} songs - An array of songs. + * @param {string} artistName - Name of the artist. + * @returns {number} Total runtime in seconds. + */ +function getTotalRuntimeOfArtist(songs, artistName) {} + +// Problem #11 +/** + * Prints artists who have more than one song in the list. + * @param {Object[]} songs - An array of songs. + */ +function printArtistsWithMultipleSongs(songs) {} + +// Problem #12 +/** + * Logs the longest song title. + * @param {Object[]} songs - An array of songs. + */ +function printLongestSongTitle(songs) {} + +// Problem #13 +/** + * Sorts songs by artist name, then by song title alphabetically. + * @param {Object[]} songs - An array of songs. + * @returns {Object[]} Sorted array of songs. + */ +function sortSongsByArtistAndTitle(songs) {} + +// 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) {} + +// Problem #15 +/** + * Finds the first song with a title starting with a specific letter. + * @param {Object[]} songs - An array of songs. + * @param {string} letter - The letter to search for. + * @returns {Object|null} The first song object that matches the criterion or null. + */ +function findFirstSongStartingWith(songs, letter) {} + +// Problem #16 +/** + * Maps each artist to an array of their song titles. + * @param {Object[]} songs - An array of songs. + * @returns {Object} An object mapping each artist to an array of their song titles. + */ +function mapArtistsToSongs(songs) {} + +// Problem #17 +/** + * Finds the album with the longest average song runtime. + * @param {Object[]} songs - An array of songs. + * @returns {string} The name of the album with the longest average song runtime. + */ +function findAlbumWithLongestAverageRuntime(songs) {} + +// Problem #18 +/** + * Logs song titles sorted by their runtime. + * @param {Object[]} songs - An array of songs. + */ +function printSongsSortedByRuntime(songs) { + +// Problem #19 +/** + * Prints a summary of each album, including its name, total runtime, and number of songs. + * @param {Object[]} songs - An array of songs. + */let albumSummaries = { + songs.forEach(song => { + if (!albumSummaries[song.album]){ + albumSummaries[song.album] ={ albumName: song.album, songCount: 1, totalRuntime: song.runtimeInSeconds}; + } + + } +} +function printAlbumSummaries(songs) {} + +// 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) {} + + + module.exports = { + getSortedTitles, + getSongsFromAlbum, + categorizeSongsByRuntime, + findAlbumWithMostSongs, + getFirstSongInAlbum, + isThereLongSong, + getSongsWithDurationInMinutes, + getAlbumsInReverseOrder, + songsWithWord, + getTotalRuntimeOfArtist, + printArtistsWithMultipleSongs, + sortSongsByArtistAndTitle, + printLongestSongTitle, + listAlbumTotalRuntimes, + findFirstSongStartingWith, + mapArtistsToSongs, + findAlbumWithLongestAverageRuntime, + printSongsSortedByRuntime, + printAlbumSummaries, + findArtistWithMostSongs + };; From b58ec00e0f96ee26de478e61ed0b4b04e85b8ab8 Mon Sep 17 00:00:00 2001 From: ExiledSPKhy Date: Sun, 14 Jan 2024 10:31:27 -0500 Subject: [PATCH 3/3] on problem 11, attempting new method. --- index.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index e96254d..757b127 100644 --- a/index.js +++ b/index.js @@ -90,7 +90,7 @@ function findAlbumWithMostSongs(songs) { let maxSongs = 0; let albumWithMostSongs; - // Iterate through albumSongCount to find the album with the most songs + for (const album in albumSongCount) { if (albumSongCount[album] > maxSongs) { maxSongs = albumSongCount[album]; @@ -132,7 +132,14 @@ 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) { + let longSongs = songs.some(song => { + if(song.runtimeInSeconds > runtime ){ + return true; + } + }) + return longSongs; +} console.log(isThereLongSong(exampleSongData)) // #7 /** @@ -148,7 +155,27 @@ 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) { + let newSarr = []; + let reversedAlbums = songs.map(song => song.album).sort().reverse(); + + reversedAlbums.forEach(song => { + if (!newSarr.includes(song)) { + newSarr.push(song); + } + }); + + //console.log(newSarr); + return newSarr; +} + +// Assuming exampleSongData is defined somewhere in your code +getAlbumsInReverseOrder(exampleSongData); + + + + // #9 /** @@ -157,7 +184,20 @@ 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 arrWords = [] + +songs.forEach(song => { + if (song.title.includes(word)){ + arrWords.push(song.title) + } + + }); + + console.log(arrWords) + return arrWords; +} + // #10 /** @@ -166,7 +206,26 @@ 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 artistSongs=[]; + let sum = 0; +//declared the sum since i knew i'd have to return a number +//created artist songs to push mrun times in to assign to sum and add up. + songs + .filter(song => song.artist === artistName) + .forEach(song => { + artistSongs.push(song.runtimeInSeconds); + sum += artistSongs; + }); + + + + return Number(sum); + + }console.log(getTotalRuntimeOfArtist(exampleSongData)); + + + // Problem #11 /**