[英]I don't know why my function Flip() in JavaScript isn't returning anything. I want it to return if it is heads or tails
JavaScript - I don't see anything obvious that is wrong JavaScript-我看不到任何明显的错误
function Flip() {
var Probability = Math.floor(Math.random());
if (Probability > 0.5) {
document.getElementById("message").innerHTML("Heads!");
}
if (Probability < 0.5) {
document.getElementById("message").innerHTML("Tails!");
}
HTML - Nothing should be wrong in the HTML (besides selectors) HTML-HTML没什么不对(选择器除外)
<body>
<button onClick="Flip()" id="submit">FLIP THE COIN</button>
<p id="message"></p>
</body>
You should assign innerHTML as below, it is not a function. 您应该按以下方式分配innerHTML,它不是一个函数。 Check demo - Fiddle .
检查演示Fiddle 。
function Flip() {
var Probability = Math.floor(Math.random());
if (Probability > 0.5) {
document.getElementById("message").innerHTML = "Heads!";
}
if (Probability < 0.5) {
document.getElementById("message").innerHTML = "Tails!";
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.