Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
233 changes: 188 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,104 @@ 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
/**
* 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) {
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
/**
Expand All @@ -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
/**
Expand All @@ -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
/**
Expand All @@ -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
/**
Expand All @@ -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
/**
Expand All @@ -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
/**
Expand Down Expand Up @@ -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
Expand All @@ -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
};;
Loading