Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.
In the Cloud Shell window on the right side of the screen, select the More icon (...), then select Settings > Go to Classic version.
Use the Azure Cloud Shell to create a file.
touch app.js
code .
Press Ctrl + S to save the file.
Give it the following starter code:
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.
Add the following code:
let cardThree = 7;
sum += cardThree;
if (sum > 21) {
console.log('You lost');
process.exit(1); // exit program
}
console.log(`You have ${sum} points`);
Save your changes, press Ctrl + Q to close the file, and then run the code.
node app.js
You see the following output:
You have 19 points
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.
Open the app.js file.
code app.js
Add the following variables to represent the banks cards.
let cardOneBank = 7;
let cardTwoBank = 5;
let cardThreeBank = 6;
let cardFourBank = 4;
Next, add the following code to the end of the file.
let bankSum = cardOneBank + cardTwoBank + cardThreeBank + cardFourBank;
if (bankSum > 21 || (sum <= 21 && sum > bankSum)) {
console.log('You win');
} else {
console.log('Bank wins');
}
Save and close the file, and then run the code.
node app.js
You see the following 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.
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');
}
Having an issue? We can help!
Please sign in to use this experience.
Sign in