使用indexOf比较数组的第一个和下一个元素。 JavaScript的

时间:2021-01-20 21:21:35

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.

我正在阅读Murach的JavaScript和JQuery书。我正在尝试创建一个彩票发生器。我目前停留在如何使用indexOf比较数组的第一个元素与下一个元素,以确保我没有重复。我的思维过程是打印5个随机数,而它之前的数字是不一样的。

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. 

4 个解决方案

#1


2  

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.

#2


1  

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.

此代码将始终确保乐透号码与之前的号码不匹配。

#3


0  

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

indexOf用于查找可在数组中找到给定元素的索引。

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]

#4


0  

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

#1


2  

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.

#2


1  

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.

此代码将始终确保乐透号码与之前的号码不匹配。

#3


0  

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

indexOf用于查找可在数组中找到给定元素的索引。

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]

#4


0  

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