lotto.js

来自「这是《JavaScript学习源代码》一书的源代码」· JavaScript 代码 · 共 30 行

JS
30
字号
// create variables
var i, rand, temp, str;

// create a new array of 50 elements
var nums = new Array(50);

// fill elements 1-49 with numbers 1-49
for(i = 1; i < 50; i++) nums[i] = i;

// algorithm to randomize these numbers
for(i = 1; i < 50; i++)
{
  rand = Math.ceil(Math.random() * 49 );
  temp = nums[i];
  nums[i] = nums[rand];
  nums[rand] = temp;
}

// add the first six elements to a string
str = "Your six lucky numbers are:\n\n";
for(i = 1; i < 7; i++)
{
  str += nums[i];
  if(i != 6) str += " - ";
}

// display the string
alert(str);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?