Skip to main content

Exercise 154

  • Create a new index154.js
  • Define a player variable and assign a literal object with the following properties and methods:
    • property: name, value: null
    • property: energy, value: 100
    • property: lives, value: 3
    • method: loseEnergy
    • method: recoverEnergy
    • method: loseLife
    • method: recoverLife

Methods

  • The loseEnergy accepts a damage numeric parameter and reduces the player object energy property value
  • The recoveryEnergy accepts a energyUp numeric parameter and restores the player object energy property value
  • The loseLife doesn't accept parameters and just subtract one player life every time you call it
  • The recoverLife doesn't accept parameters and just adds one player life every time you call it

Rules

  • The player energy can't be negative
  • The player energy can't be more than 100
  • The player lives can't be bigger thant 99
  • If the player energy is equal or lees than 0 then the player will lose a life

Play Time!!!

  1. Set the player name
  2. Player lose a life
  3. Player lose 10 energy value
  4. Player lose 5 energy value
  5. Player lose 15 energy value
  6. Player lose 20 energy value
  7. Player earn 10 energy value
  8. Player lose 30 energy value
  9. Player lose 40 energy value
  10. Player wins a life
  11. Player lose a life
  12. Player wins a life
  • Simulate each game play using the players object methods
  • Show the following output after doing all the object methods calls
The %player name% has %energy% energy % and  %lifes% lifes

The output should be:

The %player name% has 0 energy and 3 lifes

Keep doing the game with the following commands

  1. Player lose a life
  2. Player earn 10 energy value
  3. Player lose 30 energy value

The output should be:

You are dead