Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. ///////////////////////////////
  2. // DEFINE POKEMON CLASS HERE //
  3. ///////////////////////////////
  4. class Pokemon {
  5.  
  6. constructor(name,attack,defense,health,type) {
  7. this.name = name;
  8. this.attack = attack;
  9. this.defense = defense;
  10. this.health = health;
  11. this.ihealth = health;
  12. this.type = type;
  13. }
  14. takeDamage(num){
  15. this.health = (this.health) - num;
  16. if(this.health < 0)
  17. this.health = 0;
  18. }
  19. attackOpponent(opponent){
  20. var damage = this.attack - opponent.defense;
  21. if(damage < 0){
  22. damage = 1;
  23. }
  24. opponent.health = opponent.health - damage;
  25. }
  26. display(){
  27. console.log((this.name).toUpperCase()+" ("+(this.type).toUpperCase()+") "+this.health+"/"+this.ihealth);
  28. }
  29. }
  30.  
  31.  
  32. ////////////////////////
  33. // EXAMPLE TEST CASES //
  34. ////////////////////////
  35.  
  36. // #1 - Pokemon is defined
  37. console.assert(Pokemon);
  38.  
  39. // #2 - `Pokemon` object initialized with `.name`, `.attack`, `.defense`, `.health`, and `.type`
  40. const charmander = new Pokemon("charmander", 12, 8, 30, "fire");
  41. console.assert(charmander.name === "charmander", ".name property not correctly defined");
  42. console.assert(charmander.attack === 12, ".attack property not correctly defined");
  43. console.assert(charmander.defense === 8, ".defense property not correctly defined");
  44. console.assert(charmander.health === 30, ".health property not correctly defined");
  45. console.assert(charmander.type === "fire", ".type property not correctly defined");
  46.  
  47. // #3 `takeDamage()` method, which takes a number as an argument and properly reduces the `.health` of the `Pokemon` by that number
  48. charmander.takeDamage(5);
  49. console.assert(charmander.health === 25,".takeDamage() method not correctly calculating damage");
  50. charmander.takeDamage(2000);
  51. console.assert(charmander.health === 0,".takeDamage() method not correctly calculating damage");
  52.  
  53. // #4 `attackOpponent()` method, which takes a `Pokemon` object as an argument (the opponent being attacked). This method should call the `takeDamage()` method on the opponent `Pokemon`.
  54. const bulbasaur = new Pokemon("bulbasaur", 7, 9, 35, "grass/poison");
  55. charmander.attackOpponent(bulbasaur);
  56. console.assert(bulbasaur.health === 32, ".attackOpponent() method not correctly damaging opponent");
  57. bulbasaur.takeDamage = function(){this.called = true}; // stubbing .takeDamage() method;
  58. charmander.attackOpponent(bulbasaur);
  59. console.assert(bulbasaur.called, ".attackOpponent() method not properly calling the opponent's .takeDamage() method");
  60.  
  61. // #5 `display()` method, which takes no arguments and returns a string in the format "POKEMON_NAME (POKEMON_TYPE) CURRENT_POKEMON_HEALTH/ORIGINAL_POKEMON_HEALTH"
  62. const pikachu = new Pokemon("pikachu", 9, 10, 25, "electric");
  63. console.assert(pikachu.display() === "PIKACHU (ELECTRIC) 25/25", ".display() not correctly returning properly formatted string");
  64. pikachu.health = 12;
  65. console.assert(pikachu.display() === "PIKACHU (ELECTRIC) 12/25", ".display() not correctly returning properly formatted string");
  66.  
  67. console.log("If this is all you're seeing, you've passed all of the example test cases!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement