-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf.js
More file actions
40 lines (32 loc) · 1.58 KB
/
f.js
File metadata and controls
40 lines (32 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/** 6. You are developing a music playlist management system. Implement functions that leverage closures
and higher-order functions to perform common playlist operations.
Task 1: Create a function createPlaylist that takes a playlist name as a parameter and returns a closure. This
closure should allow adding and listing songs for the given playlist.
Task 2: Create a function addSong that takes a song name and artist as parameters and adds the song to the
specified playlist. Use the closure created in TASK 1.
Task 3: Create a function listSongs that lists all the songs in a specified playlist. Use the closure created in the
Task 1 */
function createPlaylist(playlistName) {
const songs = [];
function addSong(songName, artist) {
songs.push({ songName, artist });
console.log(`Added '${songName}' by ${artist} to '${playlistName}' playlist.`);
}
function listSongs() {
console.log(`Songs in '${playlistName}' playlist:`);
songs.forEach(song => {
console.log(`- ${song.songName} by ${song.artist}`);
});
}
return { addSong, listSongs };
}
const myPlaylist = createPlaylist('Chill Vibes');
myPlaylist.addSong('Sunset Lover', 'Petit Biscuit');
myPlaylist.addSong('Cold Little Heart', 'Michael Kiwanuka');
myPlaylist.listSongs();
/***output
Added 'Sunset Lover' by Petit Biscuit to 'Chill Vibes' playlist.
Added 'Cold Little Heart' by Michael Kiwanuka to 'Chill Vibes' playlist.
Songs in 'Chill Vibes' playlist:
- Sunset Lover by Petit Biscuit
- Cold Little Heart by Michael Kiwanuka */