Skip to content

Instantly share code, notes, and snippets.

@diazabdulm
Created March 4, 2019 04:39
Show Gist options
  • Save diazabdulm/9e5ae040706a0b013772ac658d6f403c to your computer and use it in GitHub Desktop.
Save diazabdulm/9e5ae040706a0b013772ac658d6f403c to your computer and use it in GitHub Desktop.
Pokemon class
// Define Pokemon class
class Pokemon {
constructor(name, attack, defense, health, type) {
this.name = name;
this.attack = attack;
this.defense = defense;
this.health = health;
this.type = type;
this.initialHealth = health;
}
takeDamage(amount) {
let newHealth = this.health - amount;
if (newHealth < 0) newHealth = 0; // health min is always 0
this.health = newHealth;
}
attackOpponent(opponent) {
let amount = this.attack - opponent.defense;
if (amount < 0) amount = 1; // attack min is always 1
opponent.takeDamage(amount);
}
display() {
// displays current Pokemon name, type, and health
// name is all upcase
let name = this.name;
name = name.toUpperCase();
// type is encapsulated within parentheses, and upcase
let type = this.type;
type = '(' + type.toUpperCase() + ')';
// current health and initial health are juxtapositioned
let health = this.health;
const initialHealth = this.initialHealth;
health = health.toString() + '/' + initialHealth.toString();
let display = name + ' ' + type + ' ' + health;
return display;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment