Search code examples
htmlcoin-flipping

Need help tossing a coin in Java


I am struggling creating my java coin toss for my webpage. I need to write a Java script to put on the webpage that will show pictures of coins being tossed and carry out the coin toss. here is what I have, why isn't it working? It just opens a new page and says "about:blank?"

<html>
<head>
<title> </title>
<script>
    function toss() {
    if (Math.random()>.5) {
        window.document.coin.src = "heads.jpeg";
    }
    else {
        window.document.coin.src = "tails.jpeg";
    }
    return false;
</script>
<body>
    <img name="coin" src="questionmark.jpeg">
    <form action="" onSubmit="return toss() ;">
        <input type="submit" value="Toss">
    </form>
</body>
</html>

Solution

  • You are missing the closing curly brace for your toss() function. Once fixed it appears to work fine. Also, yes, javascript...not java.

    function toss() {
    if (Math.random()>.5) {
    window.document.coin.src = "heads.jpeg";
    }
    else {
    window.document.coin.src = "tails.jpeg";
    }
    return false;
    }