2

I am writing a script where I have to randomly generate a number from an array and then remove the number so that it cannot be generated again. What appears to be happening is that the number generated, after being spliced, is removing other numbers from my array. I assume it is being subtracted. Here is the necessary code:

    var randNum = [0,1,2,3,4,5,6,7,8,9];

    function clickHandler ()
        {
            output = randNum[Math.floor(Math.random() * randNum.length)];
            console.log("This is the generated number:" + output);
            randNum.splice(output);
            console.log("This is the resulting array without the generated number:" + randNum);


        }
3
  • 2
    If deleteCount is omitted, or if its value is larger than array.length - start (that is, if it is greater than the number of elements left in the array, starting at start), then all of the elements from start through the end of the array will be deleted. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 8, 2018 at 20:14
  • randNum = (n => randNum.filter(x => x !== n))(Math.floor(Math.random() * randNum.length)); Trying to mutate the orignal array with .splice is unnecessary and, as you've discovered, error prone. Commented May 8, 2018 at 20:19
  • @JaredSmith that's ridiculous, splice is the most basic, this question is just about RTFM Commented May 8, 2018 at 20:24

3 Answers 3

4

You mix up value and index.

Array#splice needs a count for splicing elements. If not supplied, splice splices all items from the given index to the end.

var randNum = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

function clickHandler() {
    var index = Math.floor(Math.random() * randNum.length);
    console.log("This is the generated number: " + randNum[index]);
    randNum.splice(index, 1);
    console.log("This is the resulting array without the generated number: " + randNum);
}

clickHandler();
clickHandler();

Sign up to request clarification or add additional context in comments.

Comments

1

Use randNum.splice(index, 1); to remove only one number from array

If deleteCount is omitted, or if its value is larger than array.length - start (that is, if it is greater than the number of elements left in the array, starting at start), then all of the elements from start through the end of the array will be deleted

MDN

1 Comment

0

This is another way of doing it.

    let numbersLeft = [0,1,2,3,4,5,6,7,8,9];
    let numbersPulled = [];

    function generateNumber(){
      let randomNumber = randNum[Math.floor(Math.random() * randNum.length)];
      return randomNumber;
    }

    function clickHandler () {
      let numberToPull = generateNumber();
      if ( numbersPulled.indexOf(numberToPull) != -1){
        numbersLeft.splice(numberToTest, 0);
        numbersPulled.push(numberToTest);
      } else {
        console.log('That number has already been pulled!');
      }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.