-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIterator.js
More file actions
59 lines (48 loc) · 1.14 KB
/
Copy pathIterator.js
File metadata and controls
59 lines (48 loc) · 1.14 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Design a music playlist system that allows users to loop through songs one by one.
class AlbumIterator {
constructor(songs) {
this.songs = songs;
this.current_song_index = 0;
}
hasNext() {
return this.current_song_index < this.songs.length;
}
next() {
if(this.hasNext()) {
return this.songs[this.current_song_index++];
}
return null;
}
}
class Album {
constructor(name) {
this.name = name;
this.songs = [];
this.current_song_index = 0;
}
addSongs(song) {
this.songs.push(song);
}
iterator() {
return new AlbumIterator(this.songs);
}
}
class Song {
constructor(name, artist_name) {
this.name = name;
this.artist_name = artist_name;
}
play() {
console.info("Playing", this.name);
}
}
const tumho = new Song("tumho", "pawan");
const tumho2 = new Song("tumho2", "pawan2");
const album = new Album("saiyaara");
album.addSongs(tumho);
album.addSongs(tumho2);
const iterator = album.iterator();
while(iterator.hasNext()) {
const song = iterator.next();
song.play()
}