t1a16 — PowerUp: Stateful Hero Energy Tracker
Date: ___________________
|
Assignment: Stateful Object Assignment
Assignment Prompt
1. Define a global object called myHero with properties:
name ("Captain Code"), power (100), and
comment ("Ready for training!").
2. Complete trainHero() to subtract power every time
the training button is clicked.
3. Use if / else if / else conditions to update
myHero.comment according to the remaining power.
4. When power reaches 0 or below, trigger an alert(),
update the comment, and reset power to 100.
5. Use the button's onclick handler to call
trainHero() and display the updated object inside
myDiv01.
Hero Status
Power Classification
High Power
100%
Power remaining
Training History
Each training session is recorded below.
Training history:
Captain Code is ready for training!
Clear History
Power Level Guide
High Power: 70–100 power.
Medium Power: 45–69 power.
Low Power: 1–44 power.
Exhausted: 0 or less; power resets to 100.
These classifications are based on the hero's current power
after training.
Stateful Hero Energy Tracker | WebDev Project
Reference: MDN — Working with JavaScript objects .
Extension Challenge Add a second hero property called level. Increase it every time the hero completes five training sessions. Display the level in the status panel, and make sure resetting the hero also resets the level.
← Back to GameDev Home