Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
    <head>
        <title>Coin Flip Simulation</title>
        <link href="https://fonts.googleapis.com/css?family=Bungee+Shade|Montserrat" rel="stylesheet">
    </head>
    <body>
        <h1>Coin Flip Sim!</h1>
        <div id="wrapper">

            <div id="coin-container">
                <div id="coin">
                    <span id="heads"><svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 765.49 765.49"><defs><style>.cls-1{fill:#fffb59;stroke:#fffb59;stroke-miterlimit:10;stroke-width:5px;}.cls-2{fill:#ff404c;}</style></defs><title>coin-heads</title><circle class="cls-1" cx="382.75" cy="382.75" r="380.25"/><path class="cls-2" d="M340.39,210.36c-23.61,17.56-29.95,45.37-28.79,60.17,1.49,18.09,5.16,41.65,5.16,41.65s-7.27,3.95-7.27,19.84c2.53,39.88,15.87,22.67,18.61,40.16,6.59,42.14,21.67,34.63,21.67,57.63,0,38.31-15.8,56.22-65.11,77.45-49.48,21.32-100.49,48.15-100.49,94.61V625.1H602.3V601.87c0-46.46-51-73.29-100.54-94.59-49.32-21.23-65.06-39.12-65.06-77.45,0-23,15-15.49,21.65-57.63,2.76-17.49,16.07-.28,18.65-40.16,0-15.89-7.3-19.84-7.3-19.84s3.67-23.55,5.14-41.65c1.51-19-9.25-59.49-53.43-71.92-7.74-7.9-13-20.46,10.83-33.08-52-2.44-64.14,24.79-91.85,44.81Z" transform="translate(-10.49 -12.5)"/></svg></span>
                    <span id="tails"><svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 765.49 765.49"><defs><style>.cls-1{fill:#fffb59;stroke:#fffb59;stroke-miterlimit:10;stroke-width:5px;}.cls-2{fill:#ff404c;}</style></defs><title>coin-tails</title><circle class="cls-1" cx="382.75" cy="382.75" r="380.25"/><path class="cls-2" d="M263.61,622.78c7.2-23,18.06-55.45,32.63-102.32,63.49-10.06,89.76,8,130.08-64-32.72,10-72.17-18.58-70.06-31s91.78,8.94,150.49-74.42c-74,16.32-97.67-19.61-88.12-25,22-12.52,87.39-5.21,122.25-39.14,18-17.48,26.38-60,19.06-75.11-8.77-18.32-62.29-45.64-91.8-43.06s-75.78,112.35-89.53,111.5-16.49-49.2,7.49-94.12C360.83,197.28,314.53,232,300,261.75c-27,55.29,2.53,182.16-6.95,186.66s-41.42-58.19-50.94-86.64c-13,43.6-13.32,87.27,24.7,145.28-14.33,38.07-22.17,81.88-23.36,104.16-.57,17.8,17,21.63,20.14,11.57Z" transform="translate(-15.64 -12.5)"/></svg></span>
                </div>
            </div>

            <div id="button-container">
                <p id="notification">This toss resulted in... <span id="result"></span></p>
                <button id="flip">FLIP</button>
            </div>

            <div id="stats-container">
                <div id="heads-stats">
                    <p># of Heads: <span id="heads-stat"> </span></p>
                </div>
                <div id="tails-stats">
                        <p># of Tails: <span id="tails-stat"> </span></p>
                </div>
                <button id="reset-stats">RESET</button>
            </div>

        </div>
    </body>
</html>
              
            
!

CSS

              
                /* -------- Coin Flip Sim styles -------- */
body {
  padding:50px 25px;
  background-color:#444349;
}

h1 {
    font-family: 'Bungee Shade', cursive;
    text-align:center;
    font-size:5em;
    color:rgba(255,64,76,1);
}

#flip, #stats-container, #reset-stats {
    font-family: 'Montserrat', sans-serif;
}

#flip, #reset-stats {
    letter-spacing:1px;
    border-radius:5px;
    border-bottom-width:3px;
    outline:none;
}

#flip:hover, #reset-stats:hover {
    cursor:pointer;
}

#wrapper {
    background-color:rbga(34,33,39,1) !important;
    width:500px;
    padding:50px 50px;
    margin:auto;
    border:3px solid rgba(150,150,150,0.1);
    border-bottom-width:6px;
    border-radius:5px;
    background-color:rgba(225,225,225,1);
    text-align:center;
}

#coin-container { text-align:center; }
#coin {
    width:300px;
    height:300px;
    margin:auto;
    position:relative;
}

#heads, #tails {
    width:300px;
    height:300px;
    display:inline-block;
    backface-visibility: hidden;
    position:absolute;
    right:0;
    left:0;
    top:0;
    bottom:0;
}

#heads {
    z-index:12;
    opacity:1;
}
#tails {
    z-index:10;
    opacity:1;
}
.heads-flip-animation { animation: flip-heads 0.2s linear 0s infinite alternate; }
.tails-flip-animation { animation: flip-tails 0.2s linear 0s infinite alternate; }

@keyframes flip-heads {
    0%, 100% { transform: rotateY(0deg); }
    50% { transform: rotateY(180deg); }
  }
  
  @keyframes flip-tails {
    0%, 100% { transform: rotateY(180deg); }
    50% { transform: rotateY(0deg); }
  }

#notification {
    margin-top:25px;
    font-family: 'Montserrat', sans-serif;
    font-size:1.25em;
    color:rgba(68,67,73,1);
    opacity:0;
}

#result {
    font-family: 'Bungee Shade', cursive;
    color:rgba(255,64,76,1);
    font-size:3em;
    display:block;
}

#flip {
    margin:auto;
    margin-top:25px;
    padding:20px 50px;
    background-color:rgba(255,251,89,1);
    border-color:rgba(255,251,89,0.5);
    color:rgba(255,64,76,1);
    font-size:2.2em;
    transition:0.3s;
    letter-spacing:3px;
}

#stats-container {
    margin-top:20px;
}

#heads-stats, #tails-stats {
    display:inline-block;
    width:45%;
    background-color:rgba(240,240,240,1);
    border:1px solid rgba(150,150,150,0.25);
    border-bottom-width:3px;
    border-radius:5px;
    font-size:1.5em;
}

#heads-stat, #tails-stat { 
    display:block; 
    font-size:2.5em; 
    color:rgba(255,64,76,1);
    margin-top:10px;
}
#reset-stats {
    margin:auto;
    margin-top:20px;
    padding:10px 25px;
    font-size:1.3em;
    color:rgba(255,255,255,1);
    background-color:rgba(68,67,73,1);
    border-color:rgba(68,67,73,0.5);
}

/* -- Media Queries -- */
@media screen and (max-width:1200px) {
    body { padding-bottom:50px; }
    #reset-stats { font-size:2.5em; padding: 25px 50px; }
}
              
            
!

JS

              
                // ---- Variable Declarations
const coin = document.querySelector("#coin");
const coinHeads = document.querySelector("#heads");
const coinTails = document.querySelector("#tails");

const flipButton = document.querySelector("#flip");
const notif = document.querySelector("#notification");
const result = document.querySelector("#result");

const headsStat= document.querySelector("#heads-stat");
const tailsStat = document.querySelector("#tails-stat");

const resetStats = document.querySelector("#reset-stats");

// ---- Setting initial stat displays
headsStat.textContent = 0;
tailsStat.textContent = 0;

// ---- Removing the results if user clicks the RESET button
function resetResult() {
    notif.style.opacity = "0";
    result.textContent = "";
}

// ---- Initiating a coin flip
flipButton.addEventListener('click', function() {
    resetResult();
    coinHeads.style.opacity = "1";
    coinHeads.classList.add('heads-flip-animation');
    coinTails.classList.add('tails-flip-animation');
    flipButton.textContent = "FLIP AGAIN";
    window.setTimeout(function() {
        coinHeads.classList.remove('heads-flip-animation');
        coinTails.classList.remove('tails-flip-animation');
        getCoinFace();
    }, 1000);
});

// ---- Determining the coin face
function getCoinFace() {
    notif.style.opacity = "1";
    let face = Math.random();
    let h = parseInt(headsStat.textContent);
    let t  = parseInt(tailsStat.textContent);
    if ( face > 0.5 ) {
        headsStat.textContent = (h + 1);
        result.textContent = "HEADS!";
    } else {
        tailsStat.textContent = (t + 1);
        coinHeads.style.opacity = "0";
        result.textContent = "TAILS!";
    }
}

// ----
resetStats.addEventListener('click', function() {
    headsStat.textContent = 0;
    tailsStat.textContent = 0;
    resetResult();
    flipButton.textContent = "FLIP";
});
              
            
!
999px

Console