简体   繁体   English

我不知道为什么我的JavaScript函数Flip()不返回任何东西。 我希望它返回是正面还是反面

[英]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.

相关问题 我无法在JavaScript中使用“ \\ n”,我不知道为什么它不起作用 - I can't use '\n" in javascript, I don't know why it isn't work 我的地理位置(javascript)无法正常运行,我也不知道为什么 - my geolocation(javascript) does not work and I don't know why 我的JavaScript没有在CodePen中运行,我也不知道这是否是我的代码中的错误 - My JavaScript isn't running inside CodePen and I don't know if it's a bug with my code or not 我的素数功能坏了,但我不知道为什么 - My prime number function is broken but I don't know why 我的 function 返回 NaN 我不知道为什么 - My function returns NaN and I don´t know why 我不知道为什么我的函数不能读取quotesdata - i don't know why my function can not read quotesdata 具有externalheight函数的javascript选择器未返回任何内容,而且我也无法在其他任何地方选择选择器 - javascript selector with outerheight function isn't returning anything, and I can't select the selector anywhere else either 我的递归函数没有返回任何东西 - My recursive function isn't returning anything 我不知道为什么我的json无法生成为HTML - I don't know why my json isn't generating as HTML 我的代码中是否有任何泄漏,只有 1 个案例不起作用我不知道为什么? - Is there any leak in my code, Only 1 case isn't working I don't know why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM