我正在浏览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.
答案 0 :(得分: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.
答案 1 :(得分: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.
答案 2 :(得分:0)
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]
答案 3 :(得分: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