While迴圈
var lottery = [];
var n;
while (lottery.length<6) {
n=Math.floor(Math.random()*49)+1;
if (lottery.indexOf(n)===-1) {
lottery.push(n);
}
}
console.log(lottery);
For迴圈
var lottery = [];
var n;
for(i = 0; i < 6; i++){
n=Math.floor(Math.random()*49)+1;
if (lottery.indexOf(n)===-1) {
lottery.push(n);
}
}
console.log(lottery);
沒有留言:
張貼留言