Exercise - If...else

Completed 100 XP

This module requires a sandbox to complete. A sandbox gives you access to free resources. Your personal subscription will not be charged. The sandbox may only be used to complete training on Microsoft Learn. Use for any other reason is prohibited, and may result in permanent loss of access to the sandbox.

Microsoft provides this lab experience and related content for educational purposes. All presented information is owned by Microsoft and intended solely for learning about the covered products and services in this Microsoft Learn module.

As a junior game developer of recreational games, you've been asked to start the development of a Blackjack game. Blackjack has some interesting rules on how to calculate points. You realize this game is a great opportunity to apply some Boolean logic you've learned.

Calculate a card hand

In Blackjack, the goal of the game is to win over the bank. You win by holding a score higher than the bank but less than or equal to 21.

  1. In the Cloud Shell window on the right side of the screen, select the More icon (...), then select Settings > Go to Classic version.

  2. Use the Azure Cloud Shell to create a file.

    Bash
    touch app.js
    code .
    
  3. Press Ctrl + S to save the file.

  4. Give it the following starter code:

    JavaScript
    let cardOne = 7;
    let cardTwo = 5;
    let sum = cardOne + cardTwo; // 15
    

    Next, you'll add code that simulates taking one more card. Let's see what happens.

  5. Add the following code:

    JavaScript
    let cardThree = 7;
    sum += cardThree;
    if (sum > 21) {
      console.log('You lost');
      process.exit(1); // exit program
    }
    console.log(`You have ${sum} points`);
    
  6. Save your changes, press Ctrl + Q to close the file, and then run the code.

    Bash
    node app.js
    

    You see the following output:

    Output
    You have 19 points
    

Add an opponent

Your opponent is the bank. Remember the rules. You win if you have a better score than the bank, or if the bank has over 21. Let's implement those rules.

  1. Open the app.js file.

    Bash
    code app.js
    
  2. Add the following variables to represent the banks cards.

    JavaScript
    let cardOneBank = 7;
    let cardTwoBank = 5;
    let cardThreeBank = 6;
    let cardFourBank = 4;
    
  3. Next, add the following code to the end of the file.

    Java
    let bankSum = cardOneBank + cardTwoBank + cardThreeBank + cardFourBank;
    if (bankSum > 21 || (sum <= 21 && sum > bankSum)) {
     console.log('You win');
    } else {
      console.log('Bank wins');
    }
    
  4. Save and close the file, and then run the code.

    Bash
    node app.js
    

    You see the following output:

    Output
    You have 19 points
    You win
    

Congratulations! You've successfully implemented some of the game rules from Blackjack by using Boolean logic and operators.

Here's the full code.

JavaScript
let cardOne = 7;
let cardTwo = 5;
let sum = cardOne + cardTwo; // 15
let cardOneBank = 7;
let cardTwoBank = 5;
let cardThreeBank = 6;
let cardFourBank = 4;

let cardThree = 7;
sum += cardThree;
if (sum > 21) {
  console.log('You lost');
}
console.log(`You have ${sum} points`);

let bankSum = cardOneBank + cardTwoBank + cardThreeBank + cardFourBank;

if (bankSum > 21 || (sum <= 21 && sum > bankSum)) {
  console.log('You win');
  process.exit(1); // exit program
} else {
  console.log('Bank wins');
}

Next unit: Knowledge check

Previous Next