Ace
Ace

Reputation: 59

using indexOf to compare first and next element of array. JavaScript

I'm going through Murach's JavaScript & JQuery book. I'm trying to create a lottery generator. Im currently stuck on how to use indexOf to compare the first element of the array to the next to make sure I don't get duplicates. My thought process is to do the printing of 5 random numbers while the number before it is not the same.

var numOfTickets;
do{
   numOfTickets = prompt("Enter number of lotto tickets (1-10)"); 
   numOfTickets = parseInt(numOfTickets);

   var lottoNum = new Array(5);
   do{
      for(var i = 0; i <lottoNum.length; i++){
      var num = Math.floor(Math.random() * 47);
      lottoNum[i] = num;
   }
   document.write(lottoNum);
   }while(lottoNum.indexOf(i) != i+1)); //while the value of first index != to the next?


}
while(numOfTickets <1 || numOfTickets >10 ); //repeat prompt until valid. 

Upvotes: 1

Views: 1336

Answers (4)

HMR
HMR

Reputation: 39250

You can pick from an array of numbers and remove that item from the array then call itself until you have the right amount of numbers:

const getLottoNums = fromWhat => howMany => {
  const recur = (fromWhat, lottoNumbers ) => {
    if(lottoNumbers.length===howMany){
      //have the right amount of numbers, return it
      return lottoNumbers;
    }
    //you can never pick a number that's already been picked before because
    //  before calling recur again that number is removed from the "fromWhat" array
    const pickedNumber = fromWhat[Math.floor(Math.random() * fromWhat.length)];
    //recursively call itself removing the picked number from "fromWhat"
    return recur(//call itself again until we have enough numbers
      fromWhat.filter(x=>x!==pickedNumber),//filter out picked number
      lottoNumbers.concat([pickedNumber])//add picked number to lottoNmbers
    );
  }
  return recur(fromWhat,[]);
};

const getFrom47LottoNumbers = 
  getLottoNums(Array.from(new Array(47),(_,index)=>index+1));
document.body.addEventListener(
  "click",
  _=>console.log(getFrom47LottoNumbers(5))
);
    
click here

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You can use this code snippet to achieve what you need. Please read through the comments to get more clear on what has been done.

numOfTickets = prompt("Enter number of lotto tickets (1-10)"); 
numOfTickets = parseInt(numOfTickets);

for(var i=0; i<numOfTickets; i++){
//specify how much number you need in each lotto
var lottoLength = 5; 
//initialize each lotto to empty
var lotto = [];
//loop over the lotto length
for(var j = 0; j <lottoLength; j++){
  //get your first random lotto number
  var num = Math.floor(Math.random() * 47);
  //make sure that the lotto number do not match the number before it
  while(lotto.includes(num)){
    num = Math.floor(Math.random() * 47);
  }
  //push the number in the lotto
  lotto.push(num);
}
 //your final numOfTickets
  console.log(lotto);
}

This code will always ensure that the lotto number do not match with the number before it.

Upvotes: 1

Leo
Leo

Reputation: 1

indexOf is used to find the index at which a given element can be found in the array.

What you want to do is access the element at a particular index in the array.

You can do that by using bracket notation like this:

lottoNum[i] != lottoNum[i+1]

Upvotes: 0

xianshenglu
xianshenglu

Reputation: 5309

try this:

var numOfTickets;
do {
    numOfTickets = prompt("Enter number of lotto tickets (1-10)");
    numOfTickets = parseInt(numOfTickets);
    var lottoNum = new Array(5);

    for (var i = 0; i < lottoNum.length; i++) {
        var num;

        do {
            num = Math.floor(Math.random() * 47);
        } while (lottoNum.indexOf(num) > -1);

        lottoNum[i] = num;
    }
    document.write(lottoNum);
}
while (numOfTickets < 1 || numOfTickets > 10); //repeat prompt until valid.

Upvotes: 2

Related Questions