0

I have been reworking this a few times and thought this would resolve the issue. I am still getting two of the same numbers though. Also, the array amount will fluctuate from 8 values which I have set the array for, but then go to 9 values which I don't quite understand. Any help would be much appreciated.

function totalQuestion(){

  let amountOfQuestionTotal = myObj.length // amount of question
  var amountOfQuestionsNeeded = [8];// am
  let numberNeeded = 8;
  for(var i = 0; i < numberNeeded; i++) {
    var countNumber = Math.floor(Math.random() * amountOfQuestionTotal)
    var n = amountOfQuestionsNeeded.includes(countNumber)
    if (countNumber != n) {
      amountOfQuestionsNeeded.push(countNumber);
    }
  }

  return amountOfQuestionsNeeded;
}


totalQuestion();
console.log(totalQuestion());
Nils Kähler
  • 2,645
  • 1
  • 21
  • 26
Dr5056732
  • 29
  • 6
  • Are you trying to find a number in a specific range (like 2 to 4) or just any random integer? –  Feb 16 '21 at 18:15
  • 1
    `var amountOfQuestionsNeeded = [8];` creates an array with a single element with a value of 8. If the random number chosen is never 8, it is added to the array, *in addition to the existing 8*. That's how you get 9 elements in the resulting array. – Heretic Monkey Feb 16 '21 at 18:25
  • I did not know that thank you Heretic! – Dr5056732 Feb 16 '21 at 18:50

0 Answers0