Treasure Hunter only producing one coin

Hi! I am working on the Treasure Hunter project and the game runs fine except that only one coin is randomly generated.

Below is my code, am I missing a step? Any help is greatly appreciated!

function decideCoinLocations() {

    while(coinCollection < 5) {

      let randInx = Math.floor(Math.random() * grid.length);

      let selectedPlot = grid[randInx];

      if(coinCollection.indexOf(selectedPlot) === -1) {

        coinCollection.push(selectedPlot);

      }

    }

  };

  decideCoinLocations();        

  }

You’d need to consider what you mean should happen to make that more than one coin, and then check whether those things are being carried out.

You’re testing whether coinCollection is less than 5, and you also push to coinCollection. What type is coin collection that supports both of those operations? Usually if you test whether something is less than 5 then that something would be a number, but a number has no push method.

ahhhhh!!! I was missing the length of the array! Thank you!!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.