硬币折腾JavaScript和HTML

时间:2015-08-30 23:13:44

标签: javascript html

我需要帮助修复我的脚本。基本上,我希望它翻转硬币并用结果更新final String lozinka = new String(kasirlozinka.getPassword()); ,即它的头部或尾部。目前,当我点击按钮时没有任何反应。

JavaSript:

<span>

HTML:

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;
};

6 个答案:

答案 0 :(得分:2)

这只是因为您需要附加事件处理程序:

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;
};

答案 1 :(得分:0)

&#13;
&#13;
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;
};
&#13;
<button id="click" type="button" onclick="click()">CLICK ME</button>
<p>
    You got: <span id="result"></span>
</p>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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');

答案 3 :(得分:0)

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());

答案 4 :(得分:0)

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))

答案 5 :(得分:0)

<!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>

当然,这仍然需要一个事件处理程序;)迟到的人:)