diff --git a/index.js b/index.js index 2bb3c16..757b127 100644 --- a/index.js +++ b/index.js @@ -3,34 +3,69 @@ 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 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 /** @@ -38,7 +73,34 @@ 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 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; + + + for (const album in albumSongCount) { + if (albumSongCount[album] > maxSongs) { + maxSongs = albumSongCount[album]; + albumWithMostSongs = album; + } + } + + return albumWithMostSongs; +} +console.log(findAlbumWithMostSongs(exampleSongData)) // #5 /** @@ -47,7 +109,21 @@ function findAlbumWithMostSongs(songs) {} * @param {string} albumName - Name of the album. * @returns {Object|null} First song object in the album or null. */ -function getFirstSongInAlbum(songs, albumName) {} +function getFirstSongInAlbum(songs, albumName) { + return songs.find(song => song.album === albumName)|| 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 /** @@ -56,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 /** @@ -72,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 /** @@ -81,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 /** @@ -90,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 /** @@ -152,13 +287,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 @@ -170,25 +313,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 + };; 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 + };;