From 2e521d01cd5118bdf90ee12f0b8db1473717972a Mon Sep 17 00:00:00 2001 From: kbodur Date: Fri, 12 Jan 2024 14:04:21 -0500 Subject: [PATCH] Completed all problems --- index.js | 231 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 208 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 2bb3c16..94a42e8 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 songs = require("./data/songs"); const exampleSongData = require("./data/songs"); // Do not change the line above. @@ -13,7 +14,10 @@ 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) => {return x.title}).sort(); +} +console.log(getSortedTitles(exampleSongData)); // #2 /** @@ -22,7 +26,14 @@ 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(songs =>songs.album == albumName) + .map(songs => songs.title) +} + + + // #3 /** @@ -30,15 +41,46 @@ 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) { + let runTime= {shortSongs:0,mediumSongs:0,longSongs:0} + songs.forEach((song) =>{ + if (song.runtimeInSeconds<180){ + runTime.shortSongs++; + }else if (song.runtimeInSeconds >= 180 && song.runtimeInSeconds <= 300){ + runTime.mediumSongs++; + }else if (song.runtimeInSeconds > 300){ + runTime.longSongs++ + } + }); + return runTime; + } -// #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) {} +function findAlbumWithMostSongs(songs) { + let albumCount = {}; + songs.forEach(song => { + if (albumCount[song.album]) { + albumCount[song.album]++; + } else { + albumCount[song.album] = 1; + } + }); + let mostSongs = 0; + let albumWithMostSongs = ''; + for (let album in albumCount) { + if (albumCount[album] > mostSongs) { + mostSongs = albumCount[album]; + albumWithMostSongs = album; + } + } + + return albumWithMostSongs; +} // #5 /** @@ -47,7 +89,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) { + return songs.find(song => song.album === albumName) +} // #6 /** @@ -56,7 +100,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) { + return songs.some((song) => song.runtimeInSeconds>runtime) +} // #7 /** @@ -64,7 +110,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) { + return songs.map((song) => { song.durationInMinutes=song.runtimeInSeconds/60; +return song +}) +} // #8 /** @@ -72,7 +122,18 @@ 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 arr=songs.map((x) => x.album).sort().reverse() + let arr2=[]; + for (let i=0;i0 && arr[i]!=arr[i-1]){ + arr2.push(arr[i]) + + } + }return arr2; + } // #9 /** @@ -81,7 +142,13 @@ 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 songsTitle=[]; + for(let i=0; i x.includes(word)); + } +} // #10 /** @@ -90,21 +157,48 @@ 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 total=0; + songs.filter(song => song.artist === artistName ? total += song.runtimeInSeconds:total) +return total; + +} // 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 count={} +songs.forEach(song => { + if (count[song.artist]){ + count[song.artist]++; + }else{ + count[song.artist]=1; + } + if(count[song.artist]>1){ + console.log(song.artist); + } +}) +} // Problem #12 /** * Logs the longest song title. * @param {Object[]} songs - An array of songs. */ -function printLongestSongTitle(songs) {} +function printLongestSongTitle(songs) { + let songName=""; + let length=0; + songs.forEach(song => { + if (song.title.length>length){ + length=song.title.length; + songName=song.title; + } + }); console.log(songName); +} + // Problem #13 /** @@ -112,15 +206,35 @@ function printLongestSongTitle(songs) {} * @param {Object[]} songs - An array of songs. * @returns {Object[]} Sorted array of songs. */ -function sortSongsByArtistAndTitle(songs) {} +function sortSongsByArtistAndTitle(songs) { +// return songs.sort((A,B) => +// B.title - A.title ? -1:1).sort((A,B) => B.artist - A.artist ? -1:1) +// } +return songs.sort( + (A,B) => + A.title - B.title || + A.artist.localeCompare(B.artist) + ) +} // 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) {} +function listAlbumTotalRuntimes(songs) { + let albumTotalRuntimes={}; +songs.forEach(song =>{ + if(albumTotalRuntimes[song.album]){ + albumTotalRuntimes[song.album] +=song.runtimeInSeconds + }else{ + albumTotalRuntimes[song.album]=song.runtimeInSeconds + } +}) + + return albumTotalRuntimes; +} // Problem #15 /** @@ -129,7 +243,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) { + return songs.find(song => song.title.startsWith(letter)) || null; +} +// return songs.find(song =>song.title[0]===letter) || null; + // Problem #16 /** @@ -137,37 +255,103 @@ 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 object={}; + songs.map((song) => { object[song.artist] = (object[song.artist] || []) + .concat(song.title); + + }) + return object +} // 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) {} +function findAlbumWithLongestAverageRuntime(songs) { + let albums={}; + + songs.find((song) => { + + if (!albums[song.album]) { + albums[song.album] = { totalRuntime:song.runtimeInSeconds, songCount:1 }; + }else{ + albums[song.album].totalRuntime += song.runtimeInSeconds; + albums[song.album].songCount++; + } + }); + console.log(albums) + let albumName=''; + let average=0; + for(let i in albums){ + if(albums[i].totalRuntime/albums[i].songCount>average){ + albumName=i + average=albums[i].totalRuntime/albums[i].songCount + } + } + return albumName; +} // Problem #18 /** * Logs song titles sorted by their runtime. * @param {Object[]} songs - An array of songs. */ -function printSongsSortedByRuntime(songs) {} +function printSongsSortedByRuntime(songs) { + songs.sort((a,b) => a.runtimeInSeconds-b.runtimeInSeconds); + songs.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 albumSum={}; +songs.forEach(song => { + if (!albumSum[song.album]){ +albumSum[song.album]={ + songCount:1, + totalRuntime:song.runtimeInSeconds} + }else{ + albumSum[song.album].songCount++; + albumSum[song.album].totalRuntime +=song.runtimeInSeconds; + } +}) +for (let summary in albumSum){ + console.log( + `${summary}: ${albumSum[summary].songCount} songs, Total Runtime: ${albumSum[summary].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) { + let count = {}; + songs.forEach(song => { + if (count[song.artist]) { + count[song.artist]++; + } else { + count[song.artist] = 1; + } + }); + let max = 0; + let artistSong = ''; + for (let artist in count) { + if (count[artist] > max) { + max = count[artist]; + artistSong = artist; + } + } + + return artistSong; +} module.exports = { @@ -192,3 +376,4 @@ module.exports = { printAlbumSummaries, findArtistWithMostSongs };; +