Wanderlust challenge 41 randomise venues

So here is my code for getting four random unique venues (out of 10 fetch()'ed). Everything seems to work, I’ve tried about 20 times. I feel like this could be a bit more streamlined though, perhaps condensing the if statement with an iterator?

Any suggestions?

      const randomVenues = []
      const randomIndex = []
      while (randomIndex.length < 4) {
        let val = Math.floor(Math.random() * 10)
        if (randomIndex.includes(val)) {
        else {
      randomIndex.forEach(val => randomVenues.push(venues[val]))
      return randomVenues;

I see one potential issue. In this line: randomIndex.push(val+1); if val = 9, and already exists in your randomIndex array, you would be pushing 10 into the randomIndex array. If you only fetch 10 venues, they will have indexes of 0-9. What happens when you try to push venues[10] to your randomVenues array? Something to consider.

Hah yea I actually ran into that. For some reason I fetched 11 venues when I wrote earlier code so this works with +1 on a random array out of ten. I had originally written the random out of 11 but because of the higher value on one of the executions I got an undefined for one of my venues, hence the random out of ten

1 Like