[英]How to roll multiple dice using a for loop?
The following code rolls 5 dice at once but with 5 different variables. 以下代码一次掷出5个骰子,但有5个不同的变量。 I'm trying to use a for-loop to achieve this instead.
我正在尝试使用for循环来实现此目的。 Is this logical or do I need 5 different variables for every dice?
这是否合乎逻辑?每个骰子是否需要5个不同的变量?
function dieroll() {
var roll= "ɨ" + Math.floor(Math.random() * 6) + ";";
var roll2= "ɨ" + Math.floor(Math.random() * 6) + ";";
var roll3= "ɨ" + Math.floor(Math.random() * 6) + ";";
var roll4= "ɨ" + Math.floor(Math.random() * 6) + ";";
var roll5= "ɨ" + Math.floor(Math.random() * 6) + ";";
var roll6= "ɨ" + Math.floor(Math.random() * 6) + ";";
return roll + roll2 + roll3 + roll4 + roll5;
}
Another approach could be: 另一种方法可能是:
function dieroll() {
var result = [];
for (var i = 0; i < 5; i++){
result.push("ɨ" + Math.floor(Math.random() * 6) + ";");
}
return result;
};
With this definition, a call to dieroll() would look like: 使用此定义,对dieroll()的调用将类似于:
dieroll()
dieroll()
["⚁", "⚂", "⚂", "⚂", "⚂"]
[“⚁”,“⚂”,“⚂”,“⚂”,“⚂”]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.