Shuffle method in project "Build a Library"

Hello guys,
I finished the project at this link: https://www.codecademy.com/paths/build-web-apps-with-react/tracks/bwa-javascript-iterators-objects-and-classes/modules/learn-javascript-classes/projects/build-a-library

I tried to create the shuffle() method that should create an array of random songs. I used this code here:

class CD extends Media {
  constructor(title, artist, songs) {
    super(title);
    this._artist = artist;
    this._songs = songs;
  }

  get author() {
    return this._artist;
  }

  get songs() {
    return this._songs;
  }
 shuffle() {
    let shuffleArray = [];
    for ( let i = 0; i < this._songs.length; i++) {
    let randomNumber = Math.floor(Math.random() * ( this._songs.length - i ) );
      shuffleArray.push(this._songs[randomNumber]);
    };
    return shuffleArray;
  }

It works just fine, but there’s one main problem: the songs can be repeated inside the shuffleArray ! How can I solve this ? I tried to create an if/else but I failed at it… Can someone help me figure it out?

This was a tough one to figure out! The way I did it was by creating a second array to add used index’s too, and then using an if else statement to check this array. Try to figure that out, and if you can’t I can post in my code!

This is similar to the way I did it, though I used a while loop to check if the index already exists in the array:

Shuffle method

shuffle() { const tempArray = []; let previousIndexes = []; let newIndex; for (let i = 0; i < this.songs.length; i++) { newIndex = Math.floor(Math.random() * this.songs.length); while (previousIndexes.indexOf(newIndex) >= 0) { newIndex = Math.floor(Math.random() * this.songs.length); } tempArray.push(this.songs[newIndex]); previousIndexes.push(newIndex); } return tempArray; }

Here is a really simple if/else solution that I came up with that shuffles all of the songs from the songs array without repeating songs: