Back

Activity 1.5: Updating Variables

divider

Activity 1.5

Updating Variables

Key Concepts

Reassignment

Reassignment

A variable's value isn't locked in forever — you can give it a new value at any time by assigning to it again, without let.

Using the Old Value

let health = 100;
console.log("Health:", health);
console.log("Enemy attacks.");
health = health - 5;
console.log("Health:", health);

Using the Old Value

Terminal window
Health: 100
Enemy attacks.
Health: 95

health = health - 5; reads the current value ofhealth first, subtracts 5, then stores the result back into health.

Today's Objectives

  • Reassigning a variable's value
  • Using a variable's own current value to calculate its new value

Key Terms

Reassignment
Giving an existing variable a new value.

'F' → Fullscreen

divider

Build

Create a new JavaScript file named 1-5-updating-variables.js.


Task 1: Battle Demo

  • Track a player's and enemy's health as they attack each other.
  • Use a potion to restore some of the player's health.
1-5-updating-variables.js
console.log("--- Demo 1 - Battle Demo ---");
let playerHealth = 100;
let playerAttack = 25;
let enemyHealth = 100;
let enemyAttack = 20;
let potions = 3;
console.log("Player health:", playerHealth);
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
console.log("Player health:", playerHealth);
console.log();
console.log("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
console.log("Player health:", playerHealth);
console.log("Potions left:", potions);
console.log();

Task 2: Banking Simulator

  • Track an account balance as a deposit is made.
1-5-updating-variables.js
console.log("--- Demo 1 - Battle Demo ---");
let playerHealth = 100;
let playerAttack = 25;
let enemyHealth = 100;
let enemyAttack = 20;
let potions = 3;
console.log("Player health:", playerHealth);
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
console.log("Player health:", playerHealth);
console.log();
console.log("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
console.log("Player health:", playerHealth);
console.log("Potions left:", potions);
console.log();
console.log("--- Demo 2 - Banking Simulator ---");
let accountName = "Checking Account";
let balance = 100.50;
console.log(accountName, "balance:", balance);
console.log("Depositing 99.01 to", accountName);
balance = balance + 99.01;
console.log(accountName, "balance:", balance);
console.log();

Task 3: High Score Tracker

  • Track a player's score as points are added.
1-5-updating-variables.js
console.log("--- Demo 1 - Battle Demo ---");
let playerHealth = 100;
let playerAttack = 25;
let enemyHealth = 100;
let enemyAttack = 20;
let potions = 3;
console.log("Player health:", playerHealth);
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
console.log("Player health:", playerHealth);
console.log();
console.log("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
console.log("Player health:", playerHealth);
console.log("Potions left:", potions);
console.log();
console.log("--- Demo 2 - Banking Simulator ---");
let accountName = "Checking Account";
let balance = 100.50;
console.log(accountName, "balance:", balance);
console.log("Depositing 99.01 to", accountName);
balance = balance + 99.01;
console.log(accountName, "balance:", balance);
console.log();
console.log("--- Demo 3 - High Score Tracker ---");
let playerScore = 0;
console.log("Current score:", playerScore);
console.log("Scored 100 points!");
playerScore = playerScore + 100;
console.log("New score:", playerScore);
console.log("Scored 500 more points for a headshot!");
playerScore = playerScore + 500;
console.log("New score:", playerScore);
console.log();

Task 4: Social Media Analytics

  • Track a post's like count as more people like it.
1-5-updating-variables.js
console.log("--- Demo 1 - Battle Demo ---");
let playerHealth = 100;
let playerAttack = 25;
let enemyHealth = 100;
let enemyAttack = 20;
let potions = 3;
console.log("Player health:", playerHealth);
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Player attacks.");
enemyHealth = enemyHealth - playerAttack;
console.log("Enemy health:", enemyHealth);
console.log();
console.log("Enemy attacks.");
playerHealth = playerHealth - enemyAttack;
console.log("Player health:", playerHealth);
console.log();
console.log("Using potion...");
playerHealth = playerHealth + 10;
potions = potions - 1;
console.log("Player health:", playerHealth);
console.log("Potions left:", potions);
console.log();
console.log("--- Demo 2 - Banking Simulator ---");
let accountName = "Checking Account";
let balance = 100.50;
console.log(accountName, "balance:", balance);
console.log("Depositing 99.01 to", accountName);
balance = balance + 99.01;
console.log(accountName, "balance:", balance);
console.log();
console.log("--- Demo 3 - High Score Tracker ---");
let playerScore = 0;
console.log("Current score:", playerScore);
console.log("Scored 100 points!");
playerScore = playerScore + 100;
console.log("New score:", playerScore);
console.log("Scored 500 more points for a headshot!");
playerScore = playerScore + 500;
console.log("New score:", playerScore);
console.log();
console.log("--- Demo 4 - Social Media Analytics ---");
let likes = 23;
console.log("Post likes:", likes);
console.log("A friend liked the post!");
likes = likes + 1;
console.log("Post likes:", likes);
console.log("Another 10 people liked the post!");
likes = likes + 10;
console.log("Post likes:", likes);
divider

Checkpoint

Verify your program works correctly.

Example Output
--- Demo 1 - Battle Demo ---
Player health: 100
Enemy health: 100
Player attacks.
Enemy health: 75
Enemy attacks.
Player health: 80
Using potion...
Player health: 90
Potions left: 2
--- Demo 2 - Banking Simulator ---
Checking Account balance: 100.5
Depositing 99.01 to Checking Account
Checking Account balance: 199.51
--- Demo 3 - High Score Tracker ---
Current score: 0
Scored 100 points!
New score: 100
Scored 500 more points for a headshot!
New score: 600
--- Demo 4 - Social Media Analytics ---
Post likes: 23
A friend liked the post!
Post likes: 24
Another 10 people liked the post!
Post likes: 34
divider

Reflection

Answer the following questions before submitting your work.

  1. What's the difference between declaring a variable and reassigning it?
  2. In playerHealth = playerHealth - enemyAttack;, which side of the = is calculated first: the left or the right? Why does that order matter?
  3. Pick one of the four demos. What real-world situation could you track the same way, using a variable that changes over time?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete