Guest User

Untitled

a guest
Jul 19th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. function createCharacter(name, nickname, race, origin, attack, defense) {
  2. return {
  3. name,
  4. nickname,
  5. race,
  6. origin,
  7. attack,
  8. defense,
  9. describe : function() {
  10. return `${this.name} is a ${this.race} from ${this.origin}.`
  11. },
  12. evaluateFight : function(enemy) {
  13. // x = your character's attack - enemy's defense
  14. // y = enemy's attack - your character's defense
  15. let x = this.attack - enemy.defense
  16. let y = enemy.attack - this.defense
  17. if (x<0) {
  18. x = 'no damage'
  19. }
  20. if (y<0) {
  21. y = 'no damage'
  22. }
  23. return `Your opponent takes ${x} damage and you receive ${y} damage`;
  24. }
  25. };
  26. }
  27.  
  28. const gandalf = createCharacter('Gandalf the White', 'gandalf', 'Wizard', 'Middle Earth', 10, 6);
  29. const bilbo = createCharacter('Bilbo Baggins', 'bilbo', 'Hobbit', 'The Shire', 2, 1);
  30. //console.log(bilbo);
  31. const frodo = createCharacter('Frodo Baggins', 'frodo', 'Hobbit', 'The Shire', 3, 2);
  32. const aragorn = createCharacter('Aragorn son of Arathorn', 'aragorn', 'Man', 'Dunnedain', 6, 8);
  33. const legolas = createCharacter('Legolas', 'legolas', 'Elf', 'Woodland Realm', 8, 5);
  34. const arwen = createCharacter('Arwen Undomiel', 'arwen', 'Elf', 'Rivendell', 10, 5)
  35.  
  36. let characters = [gandalf, bilbo, frodo, aragorn, legolas];
  37. characters.push(arwen);
  38. //console.log(characters);
  39.  
  40.  
  41. const findAragorn = characters.find(myChar => {
  42. if (myChar.nickname === 'aragorn') {
  43. console.log(myChar.describe());
  44. }
  45. });
  46.  
  47. let onlyHobbits = characters.filter(myChars => myChars.race === 'Hobbit');
  48.  
  49. const highAttacks = characters.filter(attackers => attackers.attack > 5);
  50.  
  51. //console.log(gandalf.evaluateFight(bilbo));
  52. //console.log(findAragorn);
  53. //console.log(onlyHobbits);
  54. //console.log(highAttacks);
  55.  
  56. /* Equip a weapon:
  57.  
  58. function createModifiedCharacter(name, nickname, race, origin, attack, defense, weapon) {
  59. return {
  60. name,
  61. nickname,
  62. race,
  63. origin,
  64. attack,
  65. defense,
  66. weapon,
  67. describe : function() {
  68. return `${this.name} is a ${this.race} from ${this.origin} who uses a ${this.weapon}.`
  69. },
  70. evaluateFight : function(enemy) {
  71. // x = your character's attack - enemy's defense
  72. // y = enemy's attack - your character's defense
  73. let x = this.attack - enemy.defense
  74. let y = enemy.attack - this.defense
  75. if (x<0) {
  76. x = 'no damage'
  77. }
  78. if (y<0) {
  79. y = 'no damage'
  80. }
  81. return `Your opponent takes ${x} damage and you receive ${y} damage`;
  82. }
  83. };
  84. }
  85. */
Add Comment
Please, Sign In to add comment