3
真的基本功能,我有一個按鈕,模擬投擲硬幣,我想總是先顯示「頭」,然後在第一次輸入後隨機化,當前代碼不起作用,它從第一次點擊隨機化。Javascript-Rigged coin flip(頭先)
HTML:
<button id = "riggedCoin" onclick = "riggedCoinFlip()"> Rigged Coin </button>
<h1 id ="h2"> Coin Flip </h1>
JS:
function riggedCoinFlip() {
document.getElementById("h2").innerHTML = ("Heads!");
var coinFace = Math.floor(Math.random() * 2);
if (coinFace === 0) {
var h1 = document.getElementById("h2").innerHTML = ("Heads!");
} else {
var h1 = document.getElementById("h2").innerHTML = ("Tails!");
}
}