Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <div class="container">
            <h1>Welcome to Blackjack</h1>
            <h2 id="message-el">Want to play a round?</h2>
            <p>Cards: <span id="cards"></span></p>
            <p>Sum: <span id="sum-el"></span></p>
            <button onclick="startGame()">START GAME</button>
            <button onclick="newCard()">NEW CARD</button>
            <p>Per: </p>
        </div>
              
            
!

CSS

              
                body {
    margin: auto;
    margin-top: 18vh;
    background-image: url(cards.jpg);
    background-size: cover;
    
}
h1 {
    color: goldenrod;
}
h2, p {
    color: white;
}
h2 {
    font-style: italic;
}
.container {
    min-width: 75vw;
    max-height: 75vh;
    border: solid 3px black;
    border-radius: 15px;
    text-align: center;
    margin: 12.5%;
    background-color: #016f32; 
}
button {
    display: block;
    margin: auto;
    min-width: 35vw;
    min-height: 4.5vh;
    color: #016f32;
    font-weight: bold;
    background-color: goldenrod;
    border: none;
    border-radius: 3px;
    margin-top: 2.5%;
}
button:hover {
    background-color: yellow;
}
@media only screen and (min-width: 1024px) {
    .container {
        display: block;
        min-width: 5%;
    }
    button {
        min-height: 7.5vh;
        /* max-width: 5px; */
        font-weight: bolder;
    }
}
              
            
!

JS

              
                var hasBlackJack = false; 
var isAlive = true; 
var message = "";
const faceCards = ['J', 'Q', 'K']; 
var thirdCard = ""; 

var sum = 0; 
var min = 2; 
var max = 11; 

function startGame() { 

    let firstCard = Math.floor(Math.random() * (max - min) + min);  
    let secondCard = Math.floor(Math.random() * (max - min) + min);
    
    sum = firstCard + secondCard; 

    if (firstCard == 10) {
        firstCard = faceCards[Math.floor(Math.random() * (faceCards.length))]
    }
    if (secondCard == 10) {
        secondCard = faceCards[Math.floor(Math.random() * (faceCards.length))]
    }

    document.getElementById("cards").innerHTML = firstCard + " " + secondCard + " " + thirdCard;
    document.getElementById("sum-el").innerHTML = sum; 
        
    if (sum < 21) {
        message = "Would you like a new card?"; 
        document.getElementById("message-el").innerHTML = message; 
    } 
    else if (sum === 21) {
        message = "You've got blackjack!";
        document.getElementById("message-el").innerHTML = message; 
        hasBlackJack = true; 
    }
    else {
        message = "You lose!";
        document.getElementById("message-el").innerHTML = message; 
        isAlive = false; 
    }


}
function newCard() {
    
    thirdCard = Math.floor(Math.random() * (max - min) + min);
    // sum += thirdCard; 
    startGame(); 
}

              
            
!
999px

Console