Coin toss met javascript en html

Ik heb hulp nodig om mijn script te bevestigen. Kortom, ik wil dat het een muntstuk omdraait en een <span>met het resultaat, d.w.z. als het hoofdtaarten of staarten zijn. Op dit moment gebeurt er niets wanneer ik op de knop klik.

JAVASRIPt:

var heads = 0;
var tails = 0;
function click() {  
    x = (Math.floor(Math.random() * 2) == 0);
    if(x){
        flip("heads");
    }else{
        flip("tails");
    }
};
function flip(coin) {
    document.getElementById("result").innerHTML = coin;
};

HTML:

<button id="click" type="button">CLICK ME</button>
<p>
    You got: <span id="result"></span>
</p>

Antwoord 1, Autoriteit 100%

Dat is gewoon omdat je de gebeurtenishandler moet bijvoegen:

document.getElementById('click').onclick = click;
var heads = 0;
var tails = 0;
function click() {  
    x = (Math.floor(Math.random() * 2) == 0);
    if(x){
    	flip("heads");
    }else{
        flip("tails");
    }
};
function flip(coin) {
    document.getElementById("result").innerHTML = coin;
};
<button id="click" type="button">CLICK ME</button>
<p>
    You got: <span id="result"></span>
</p>

Antwoord 2, Autoriteit 12%

var prob1 = Math.floor(Math.random() * 2) +1;
var prob2 = Math.floor(Math.random() * 2) +1;
if( prob1 === prob2){
   document.write('You Got TAIL');
 }else{
   document.write('You Got HEAD');

Antwoord 3

document.getElementById('click').onclick = click;
var heads = 0;
var tails = 0;
function click() {  
    x = (Math.floor(Math.random() * 2) == 0);
    if(x){
    	flip("heads");
    }else{
        flip("tails");
    }
};
function flip(coin) {
    document.getElementById("result").innerHTML = coin;
};
<button id="click" type="button" onclick="click()">CLICK ME</button>
<p>
    You got: <span id="result"></span>
</p>

Antwoord 4

function flip(Throws) {
let coinArray = [];
for (var i = 0; i < Throws; i++) {
rndNo = Math.floor(Math.random() * 2 + 1);
if (rndNo === 1) {
Toss = 'H';
} else {
Toss = 'T'
} // if (rnd === 1)
coinArray.push(Toss);
} 
return coinArray;
}
console.log(flip(10));
function flipHeadCoin() {
let coinArray = [];
let numHeads = 0;
do {
rndNo = Math.floor(Math.random() * 2 + 1);
// ternary operators
Toss = (rndNo === 1) ? 'H' : 'T';
numHeads += (Toss === 'H') ? 1: 0;
coinArray.push(Toss);
} while (numHeads < 6);
return coinArray;
}
console.log(flipHeadCoin());

Antwoord 5

const coinFlip = num_of_coin_flips => {
  heads = 0;
  tails = 0;
  for(i = 0; i < num_of_coin_flips; i++){
   randomNumber = Math.floor(Math.random() * 2);
   roundNum = Math.round(randomNumber* 100) / 100;
   roundNum === 0
   ? heads += 1
   : tails += 1;
  }
  return `There are ${heads} heads and ${tails} tails. That's a ${((heads / num_of_coin_flips) * 100)} % / ${((tails / num_of_coin_flips) * 100)} % split`
}
console.log(coinFlip(3))

Antwoord 6

<!DOCTYPE html>
<html>
<body>
<p id="Another way"></p>
<script>
var x; 
x = (Math.floor(Math.random() * 2) == 0);
if  (x==1) {
x="Blue"
} else {
x="Red"
};
document.getElementById("Another way").innerHTML =
"The color of the pill is " + x + "!.";
</script>
</body>
</html>

En natuurlijk heeft dit nog steeds een gebeurtenishandler nodig;) Het is laat jongens:)

Other episodes