148k views
1 vote
Write a function called removePartyKillers that takes in an array like "playlist" (see below) and returns a copy of that playlist where any song longer than 8 minutes has been removed. You can safely assume that a playlist is an array of objects. Each object will have the properties title, artist, and durationInSeconds.

var awesomePlaylist = [
{
title: "Hay Day",
artist: "Robo-Crop",
durationInSeconds: 378
}, {
title: "10,000 Pounds",
artist: "L-Ton Jonn",
durationInSeconds: 498,
}, {
title: "Caesar's Salad",
artist: "DJ Dinner Julius",
durationInSeconds: 600,
}, {
title: "The British Are On Their Way",
artist: "Raul Pevere",
durationInSeconds: 1095,
}, {
title: "13th",
artist: "The Doctors",
durationInSeconds: 185,
}
];
function removePartyKillers(playlist) {

}
removePartyKillers(awesomePlaylist);
Would return:

[
{
title: "Hay Day",
artist: "Robo-Crop",
durationInSeconds: 378
}, {
title: "13th",
artist: "The Doctors",
durationInSeconds: 185,
}
]

User Eoinzy
by
3.5k points

1 Answer

4 votes

Answer:

function removePartyKillers(playlist){

for(var i=0; i < playlist.length; i++){

if((playlist[i].durationInSeconds) > 480){

playlist.splice(i, 1);

}

}

console.log(playlist);

}

Step-by-step explanation:

When 8 minutes is converted to seconds, we have 480 seconds, that is 60 * 8 = 480. So, song whose duration is greater than 480 is removed.

The function has a for loop that loop through the entire playlist.

Inside the for loop, there is an if statement that check if for each song the durationInSeconds is greater than 480; once a durationInSeconds is greater than 480, that particular element is removed using the splice inbuilt method.

The splice method takes two arguments, the index at which to remove/add an element and the second element which is the number of elements to remove/add. The splice method returns a modified array.

Lastly, the modified array is logged to the console.

User Yohei Onishi
by
3.9k points