该代码应将10次翻转的大部分奖励给获胜者硬币一侧。 但是,它只会进行9次翻转,并奖励9次中的大多数。
我尝试调整数字,但没有一个成功。将百分比更改为110后,总的翻转次数仅为7
//start decleration of variables; self explainitory
var placeholder = document.getElementById("placeholder");
var heads_counter = 0;
var tails_counter = 0;
var wins = 10;
//reset button listener & functionality
document.getElementById("reset").addEventListener("click", function(){
heads_counter = 0;
tails_counter = 0;
placeholder.innerText = ("");
coin_placeholder.innerText = ("");
});
//coin img event listener & functionality
document.getElementById("coin").addEventListener("click", function(){
//50% chance of 1 or 2
var chance = Math.floor(Math.random() * 2);
//if chance is 1 "Heads!"" is displayed in place holder and adds to heads_counter
if (chance == 1){
placeholder.innerText = ("Heads!");
heads_counter++;
//else statment if chance is 2, "Tails!" is displayed and adds to tails_counter
}else {
placeholder.innerText = ("Tails!");
tails_counter++;
}
//if the majority of total flips is heads, "Heads wins!" is displayed
if(51 <= heads_counter / wins * 100 && tails_counter / wins * 100 <= 49){
placeholder.innerText = ("Heads Wins!");
heads_counter = 0;
tails_counter = 0;
//if the majority of total flips is tails, "Tails wins!" is displayed
}else if (51 <= tails_counter / wins * 100 && heads_counter / wins * 100 <= 49){
placeholder.innerText = ("Tails Wins!");
heads_counter = 0;
tails_counter = 0;
//if flips are tied, "Tie!" is displayed
}else if(tails_counter / wins * 100 == 50 && heads_counter / wins * 100 == 50){
placeholder.innerText = ("Tie!")
heads_counter = 0;
tails_counter = 0;
}
//innerText of coin_placeholder
coin_placeholder.innerText = ("Heads: " + heads_counter + " Tails: " + tails_counter);
});
答案 0 :(得分:1)
掷硬币没有发生十次。
您在第二条if
语句中的逻辑实际上是在一侧寻找5个或更多的翻转,但不检查所需的余数。即即使只有1个尾巴,也将有6个正面获胜并结束比赛。
这是因为大于和小于需求不一定总和为100%,即6个正面和3个反面将满足这些要求,因为60%为正面,而30%为反面(即> 51%正面和反面<49%)。
我添加了基本检查,如果尚未达到整体限制,则退出此检查之前的功能。
//start decleration of variables; self explainitory
var placeholder = document.getElementById("placeholder");
var coin_placeholder = document.getElementById("coin_placeholder");
var heads_counter = 0;
var tails_counter = 0;
var wins = 10;
//reset button listener & functionality
document.getElementById("reset").addEventListener("click", function() {
heads_counter = 0;
tails_counter = 0;
placeholder.innerText = ("");
coin_placeholder.innerText = ("");
});
//coin img event listener & functionality
document.getElementById("coin").addEventListener("click", function() {
//50% chance of 1 or 2
var chance = Math.floor(Math.random() * 2);
//if chance is 1 "Heads!"" is displayed in place holder and adds to heads_counter
if (chance == 1) {
placeholder.innerText = ("Heads!");
heads_counter++;
//else statment if chance is 2, "Tails!" is displayed and adds to tails_counter
} else {
placeholder.innerText = ("Tails!");
tails_counter++;
}
//innerText of coin_placeholder
coin_placeholder.innerText = ("Heads: " + heads_counter + " Tails: " + tails_counter);
// Check if total coin flips matches the required number of wins, exit function if it does not
if ( heads_counter + tails_counter < wins ) {
return;
}
//if the majority of total flips is heads, "Heads wins!" is displayed
if (51 <= heads_counter / wins * 100 && tails_counter / wins * 100 <= 49 ) {
placeholder.innerText = ("Heads Wins!");
//if the majority of total flips is tails, "Tails wins!" is displayed
} else if (51 <= tails_counter / wins * 100 && heads_counter / wins * 100 <= 49 ) {
placeholder.innerText = ("Tails Wins!");
//if flips are tied, "Tie!" is displayed
} else if (tails_counter / wins * 100 == 50 && heads_counter / wins * 100 == 50 ) {
placeholder.innerText = ("Tie!")
}
// This can come out of the if statement above now\
// Reset counters
heads_counter = 0;
tails_counter = 0;
});
<p id="placeholder"></p>
<p id="coin_placeholder"></p>
<button id="reset">Reset</button>
<button id="coin">Coin</button>