为什么我需要用数学.石头剪子布游戏的数组层数


var arrayName = ['Rock', 'Paper', 'Scissor'];
var selection = arrayName[Math.floor(Math.random()*arrayName.length)];
alert('The computer chose:' + selection);

Math.random()返回一个分数,当您将其乘以数组的长度时,您将得到一个带有小数点的数字。但数组索引必须是整数。Math.floor()省略小数点后的分数

最新更新