samedi 25 février 2017

How to generate 4 random non-repeating numbers from 0-9?

I need help with this, what am i doing wrong? I've tried everything i know yet i still get the wrong numbers being popped and 9 is a real headache. I want to get a random digit from 0-9 and have it popped so it doesn't get repeated but i find that after the the second number is pushed it doesn't have it's number popped. Instead, some other number not yet selected is popped giving room for a reapeat.

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

function chooseRandomNumber() {
  return choose[Math.floor(Math.random() * choose.length)];
}

for (var i = 0; i < 4; i++) {
  if (i === 0) {
    yourNum.push(chooseRandomNumber());
    if (yourNum[yourNum.length - 1] === 9) {
      choose.pop();
    } else {
      choose.splice(yourNum[0], 1);
    }
  } else if (i === 1) {
    yourNum.push(chooseRandomNumber());
    if (yourNum[yourNum.length - 1] === 9) {
      choose.pop();
    } else {
      choose.splice(yourNum[1], 1);
    }
  } else if (i === 2) {
    yourNum.push(chooseRandomNumber());
    if (yourNum[yourNum.length - 1] === 9) {
      choose.pop();
    } else {
      choose.splice(yourNum[2], 1);
    }
  } else if (i === 3) {
    yourNum.push(chooseRandomNumber());
    if (yourNum[yourNum.length - 1] === 9) {
      choose.pop();
    } else {
      choose.splice(yourNum[3], 1);
    }
  }
}

console.log(choose);
console.log(yourNum);



Aucun commentaire:

Enregistrer un commentaire