Get Random Songs(Strings) from Array

Hi guys,

I have set up a parent class and child class for MEdia and CDs below. I am trying to log a random song from the song array. I keep getting errors. Please can someone et me know what I am doing wrong. I have tried making a function to randomise the songs and call it at the bottom fo the code but it is not working.

class Media { constructor(title){ this._title = title; this._isCheckedOut = false; this._ratings = []; } get title() { return this._title; } get isCheckedOut() { return this._isCheckedOut; } get ratings() { return this._ratings; } set isCheckOut(newIsCheckout) { this._isCheckOut = newIsCheckout; } toggleCheckOutStatus() { this._isCheckedOut = !this._isCheckedOut; } getAverageRating() { let ratingsWoolley = this.ratings.reduce((accumulator, rating) => accumulator + rating); return ratingsWoolley / this.ratings.length; } addRating(NewlyAddedRating) { this._ratings.push(NewlyAddedRating); } } class CD extends Media { constructor(artist,title,songs){ super(title); this._artist = artist; this._songs = []; } get artist() { return this._artist; } get songs() { return this._songs; } addSong(NewlyAddedSong) { this._songs.push(NewlyAddedSong); } let randomSong = songs[Math.floor(Math.random()*songs.length)]; //function shuffle(){ // let randomSong = songs[Math.floor(Math.random()*songs.length)]; //} } const theSmashingPumpkins = new CD('The Smashing Pumpkins', 'Greatest Hits', 18); console.log('Is the CD Checked Out?', theSmashingPumpkins.isCheckedOut); theSmashingPumpkins.addRating(4); theSmashingPumpkins.addRating(4); theSmashingPumpkins.addRating(5); theSmashingPumpkins.addRating(4); console.log('Ratings for The Smashing Pumpkins:', theSmashingPumpkins.getAverageRating()); theSmashingPumpkins.addSong('1979'); theSmashingPumpkins.addSong('Hey Yeah'); theSmashingPumpkins.addSong('Butterfly'); console.log('Songs of The Smashing Pumpkins:', theSmashingPumpkins.songs); console.log('Songs of The Smashing Pumpkins:', theSmashingPumpkins.randomSong);

Hi there,

//You're starting with this:
function shuffle() {
}

With “function shuffle()” it will throw an unexpected identifier–because just like addSong(), you want to create a method for the CD class and not a function.

//Within your method you have this:
let randomSong = songs[Math.floor(Math.random()*songs.length)];

This throws an error saying that “songs” is not identified–which is correct–“songs” has not been identified within the method but it is identified within the class. Again, look at the method addSong() and you should be able to see what it is that you are missing.

And then finally, because you are creating a value that needs to be called on–remember to return randomSong within the shuffle() method.

1 Like

Hi Kirativewd, I have made a few ammeSaturday ndments now. But still banging my head against the wall. Please can you let me know what you think is wrong with the code

class Media { constructor(title){ this._title = title; this._isCheckedOut = false; this._ratings = []; } get title() { return this._title; } get isCheckedOut() { return this._isCheckedOut; } get ratings() { return this._ratings; } set isCheckOut(newIsCheckout) { this._isCheckOut = newIsCheckout; } toggleCheckOutStatus() { this._isCheckedOut = !this._isCheckedOut; } getAverageRating() { let ratingsWoolley = this.ratings.reduce((accumulator, rating) => accumulator + rating); return ratingsWoolley / this.ratings.length; } addRating(NewlyAddedRating) { this._ratings.push(NewlyAddedRating); } } class CD extends Media { constructor(artist,title,songs){ super(title); this._artist = artist; this._songs = []; } get artist() { return this._artist; } get songs() { return this._songs; } addSong(NewlyAddedSong) { this._songs.push(NewlyAddedSong); } shuffle(){ let randomSong = this._songs[Math.floor(Math.random()*this._songs.length)]; return randomSong; } } const theSmashingPumpkins = new CD('The Smashing Pumpkins', 'Greatest Hits', 18); console.log('Is the CD Checked Out?', theSmashingPumpkins.isCheckedOut); theSmashingPumpkins.addRating(4); theSmashingPumpkins.addRating(4); theSmashingPumpkins.addRating(5); theSmashingPumpkins.addRating(4); console.log('Ratings for The Smashing Pumpkins:', theSmashingPumpkins.getAverageRating()); theSmashingPumpkins.addSong('1979'); theSmashingPumpkins.addSong('Hey Yeah'); theSmashingPumpkins.addSong('Butterfly'); console.log('Songs of The Smashing Pumpkins:', theSmashingPumpkins.songs); console.log('Songs of The Smashing Pumpkins:', theSmashingPumpkins.shuffle);
1 Like

Of course!

On your last line you have:

console.log('Songs of The Smashing Pumpkins:', theSmashingPumpkins.shuffle);

Because you want to call the method, you want to add ( )

console.log('Songs of The Smashing Pumpkins:', theSmashingPumpkins.shuffle());
1 Like

Unbelievable! I was looking and working on the wrong part for ages. I must have recoded the method about 20 x! Thanks mate. I will never forget that error again

1 Like