Sports Game Example

Here is an example of how numbers might be used in the context of a sports game:

let score = 0;

function addPoints(points) {
  score += points;
  console.log(`You scored ${points} points! Your total is now ${score}.`);
}

addPoints(5); // Outputs "You scored 5 points! Your total is now 5."
addPoints(3); // Outputs "You scored 3 points! Your total is now 8."

let timeRemaining = 60;

function subtractTime(seconds) {
  timeRemaining -= seconds;
  console.log(`${seconds} seconds have passed. Time remaining: ${timeRemaining} seconds.`);
}

subtractTime(15); // Outputs "15 seconds have passed. Time remaining: 45 seconds."
subtractTime(30); // Outputs "30 seconds have passed. Time remaining: 15 seconds."

In this example, we have a variable called score that stores the player's current score. We have a function called addPoints that adds a given number of points to the player's score and displays a message to the player with their updated total.

We also have a variable called timeRemaining that stores the amount of time left in the game, and we have a function called subtractTime that subtracts a given number of seconds from the time remaining and displays a message to the player with the updated time.

Last updated