Conditionals
Practice simple JavaScript conditionals with players and coins.
JavaScript Conditionals
1. Reference Guide
Key Vocabulary
| Term | Meaning | Example |
|---|---|---|
| Condition | A question that is true or false. | coins >= 10 |
if |
Runs code when its condition is true. | if (coins >= 10) { ... } |
else |
Runs code when the if condition is false. |
else { ... } |
| Code block | Code inside curly braces { }. |
{ console.log("Hello"); } |
A game can use a condition to decide whether a player has enough coins to buy something.
2. LxD Cycle
Define: Players need a simple way to understand how a game makes decisions.
Learning Goal: Use an if...else statement to check whether a player has enough coins.
Ideate: How can a game respond differently when a player has enough coins and when they do not?
Activity: Use player names and coin totals to display a simple game message.
3. CS111 Requirements
This lesson practices introductory programming control structures by using a condition to choose which code runs. Students will read, change, and write a simple JavaScript if...else statement.
4. Lesson Plan
Learning Objective: Use if and else to make a simple decision about a player’s coins.
Success Criteria: You can identify the condition and write a message for both outcomes.
Tech Talk
A player wants to buy a power-up that costs 10 coins. The game checks coins >= 10. If the condition is true, the player can buy it. Otherwise, the else block runs.
5. Code Example
Run the code. Change the player’s name or coin total, then run it again to see how the decision changes.
A. Checking a Player’s Coins
Code Runner Challenge
Change the name or coin total and run the code.
View IPYNB Source
%%js
// CODE_RUNNER: Change the name or coin total and run the code.
const playerName = "Alex";
const coins = 8;
if (coins >= 10) {
console.log(playerName + " can buy a power-up!");
} else {
console.log(playerName + " needs more coins.");
}
6. Hacks & Practice Tasks
Complete the two Popcorn Hacks and the Homework Hack. Run each code runner to check your work.
When you finish, use the Submit Assignment form at the bottom of this page to submit the saved code from the runners.
Popcorn Hack 1: Can the Player Buy It?
Complete the if...else so the game tells Alex whether they have enough coins to buy the power-up. The power-up costs 10 coins.
Code Runner Challenge
Complete the if...else statement.
View IPYNB Source
%%js
// CODE_RUNNER: Complete the if...else statement.
const playerName = "Alex";
const coins = 12;
// If the player has at least 10 coins, say they can buy the power-up.
// Otherwise, say they need more coins.
Popcorn Hack 2: Check Two Players
Write an if...else for each player. Each player needs at least 10 coins to buy a power-up.
Code Runner Challenge
Check whether each player has at least 10 coins.
View IPYNB Source
%%js
// CODE_RUNNER: Check whether each player has at least 10 coins.
const playerOne = "Alex";
const playerOneCoins = 8;
const playerTwo = "Alex";
const playerTwoCoins = 12;
// Add an if...else for playerOne.
// Add an if...else for playerTwo.
Homework Hack: Help Both Players
The shop’s power-up costs 10 coins. Finish the code so both players get a message saying whether they can buy it. Change the coin totals and test your code.
Code Runner Challenge
Complete both if...else checks.
View IPYNB Source
%%js
// CODE_RUNNER: Complete both if...else checks.
const playerOne = "Alex";
const playerOneCoins = 8;
const playerTwo = "Alex";
const playerTwoCoins = 12;
// Check whether playerOne has enough coins.
// Check whether playerTwo has enough coins.
Grading Plan
This assignment is worth 1 point total:
- 0.25 pts: Popcorn Hack 1 correctly checks one player’s coins.
- 0.25 pts: Popcorn Hack 2 checks both players with
if...elsestatements. - 0.5 pts: Homework Hack gives the correct result for both players and runs without errors.
7. References
W3Schools. JavaScript If…Else Statements.
Geeks for Geeks. geeksforgeeks.com
8. Lesson Revisions
9. Feedback
Submit Assignment
Need to update a submission later? Open the submissions dashboard.