MusicPlayer exercise on JavaScript

parent 8acbe1a2
class Player {
constructor() {
this.tracks = [];
this.actualTrack = 0;
this.tracks = []; // the track list
this.actualTrack = 0; // the index of which song on the track list is playing
}
}
......@@ -13,40 +13,40 @@ class Track {
}
}
let player = new Player();
let driveTrack = new Track('Incubus', 'Drive', 'Make Yourself');
let laBambaTrack = new Track('Ritchie Valens', 'La Bamba', 'La Bamba');
let player = new Player(); // create my playlist
let driveTrack = new Track('Incubus', 'Drive', 'Make Yourself'); // create a track
let laBambaTrack = new Track('Ritchie Valens', 'La Bamba', 'La Bamba'); // create a track
Player.prototype.add = function(string) {
Player.prototype.add = function(string) { // to add tracks to my playlist
this.tracks.push(string);
}
Player.prototype.play = function() {
console.log(`Playing: ${this.tracks[this.actualTrack].title} by ${this.tracks[this.actualTrack].artist}`);
console.log(`Playing: ${this.tracks[this.actualTrack].title} by ${this.tracks[this.actualTrack].artist}`); // display the track that is current "playing"
}
Player.prototype.next = function() {
if (this.actualTrack < this.tracks.length-1) {
if (this.actualTrack < this.tracks.length-1) { // to play next song on the list
this.actualTrack = this.actualTrack + 1;
}
else {
this.actualTrack = 0;
this.actualTrack = 0; // in case the song is the last one, go back to the first of the list
}
}
Player.prototype.previous = function() {
if (this.actualTrack > 0) {
if (this.actualTrack > 0) { // to play next song on the list
this.actualTrack = this.actualTrack - 1;
}
else {
this.actualTrack = this.tracks.length -1;
this.actualTrack = this.tracks.length -1; // in case the song is the first one, go back to the last of the list
}
}
player.add(driveTrack); // add 'Incubus', 'Drive', 'Make Yourself'
player.add(driveTrack); // add track driveTrack to my playlist
player.play(); // display "Drive by Incubus"
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment