我编写了一个函数,用于在单击按钮时根据百分比生成结果,问题是我需要该函数重复 10 次,因此按钮的名称。迄今roll10.onclick = function generate() { var outcome = ""; var percent = r(); if (percent >= 0 && percent < 1) { outcome = "Outcome 1"; } else if (percent >= 1 && percent < 10) { outcome = "Outcome 2"; } else if (percent >= 10 && percent < 30) { outcome = "Outcome 3"; } else { outcome = "Outcome 4"; } $("#results").append("<div class='result'>" + outcome); }
1 回答
幕布斯6054654
TA贡献1876条经验 获得超7个赞
你应该学习 for 循环。
就这么简单:
roll10.onclick = function generate() {
for(let i = 0; i < 10; i++){
var outcome = "";
var percent = r();
if (percent >= 0 && percent < 1) {
outcome = "Outcome 1";
} else if (percent >= 1 && percent < 10) {
outcome = "Outcome 2";
} else if (percent >= 10 && percent < 30) {
outcome = "Outcome 3";
} else {
outcome = "Outcome 4";
}
$("#results").append("<div class='result'>" + outcome);
}
}
添加回答
举报
0/150
提交
取消