Shuffle Error ? Build a Library

I think i F up the program , would you help me out here

Code :

       class CD extends Media {
  constructor(title,artist,trackCount) {
    super(title)
    this._artist = artist
    this._trackCount = [] 
  }
  
  get artist() {
    return this._artist
  }
  
  get trackCount() {
		return this._trackCount
  }
  
  addTrack(trac) {
    this._trackCount.push(trac)
  }

  shuffle() {
		const a = this._trackCount.length
    for (let i = 0; i < a ; i++) {
      const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
    }
  }

const test = new CD('INVALID' , 'INVALID' , 2)
console.log('                                       ')
test.addTrack(['Cant Hold US' , 'Cukup Tau'])
console.log(test.trackCount)
console.log(test.shuffle())

Error :
UNDEFINED

Yo, if you could please help me out as quick as possible thanks!

shuffle doesn’t return anything, so it returns undefined.

so by calling console.log() when you call shuffle, you log this returned result to the console. Just get rid of console.log when calling shuffle to not log returned result

Done,then what am i supposed to do next?

i would first fix your indent before moving on. The indent looks like a mess.

Just like my life :cry:

well, indent is fixable so then your life should be fixable as well, making the same analogy.

ayeee JK man,loving life

impossible, programmers do not have enjoyable life.

spoiler: just kidding

Nice, what am i supposed to do know

simple:

2 Likes

I Don’t know if you have already figured it out, but first, as stetim said, you need to return your list. Secondly, the way I did it, I would

for (let i = a; i > 0; i–)

I don’t know why this way worked for me but your way didn’t, but alas, such is programming. I tried it your way first also and it didn’t work. but when I reversed it all, it did.

To be more specific, here is my code which worked:

  shuffle() {
    for (let i = this.songs.length - 1; i > 0; i--) {
      let j = Math.floor(Math.random() * (i + 1));
      [this.songs[i], this.songs[j]] = [this.songs[j], this.songs[i]]
    }
    return this.songs;
  }
1 Like