Advertisement
ErolKZ

Untitled

Feb 21st, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. class SummerCamp {
  2.  
  3.  
  4. constructor(organizer, location) {
  5.  
  6. this.organizer = organizer;
  7.  
  8. this.location = location;
  9.  
  10. this.priceForTheCamp = { "child": 150, "student": 300, "collegian": 500 };
  11.  
  12. this.listOfParticipants = [];
  13.  
  14. }
  15.  
  16.  
  17.  
  18. registerParticipant(name, condition, money) {
  19.  
  20. let arrOfConditions = Object.keys(this.priceForTheCamp);
  21.  
  22. let namesOfPart = this.listOfParticipants.map(obj => obj.name);
  23.  
  24. if (!arrOfConditions.includes(condition)) {
  25.  
  26. throw new Error("Unsuccessful registration at the camp.");
  27.  
  28.  
  29.  
  30. } else if (namesOfPart.includes(name)) {
  31.  
  32. return `The ${name} is already registered at the camp.`;
  33.  
  34. } else if (this.priceForTheCamp[condition] > money) {
  35.  
  36. return `The money is not enough to pay the stay at the camp.`;
  37.  
  38. } else {
  39.  
  40. this.listOfParticipants.push({ name: name, condition: condition, power: 100, wins: 0 });
  41.  
  42. return `The ${name} was successfully registered.`;
  43.  
  44. }
  45.  
  46. }
  47.  
  48.  
  49. unregisterParticipant(name) {
  50.  
  51. let namesOfPart = this.listOfParticipants.map(obj => obj.name);
  52.  
  53. if (!namesOfPart.includes(name)) {
  54.  
  55. throw new Error(`The ${name} is not registered in the camp.`);
  56.  
  57.  
  58.  
  59. } else {
  60.  
  61. this.listOfParticipants.forEach((obj, i) => obj.name === name ? this.listOfParticipants.splice(i, 1) : false);
  62.  
  63. return `The ${name} removed successfully.`;
  64.  
  65. }
  66.  
  67. }
  68.  
  69.  
  70.  
  71. timeToPlay(typeOfGame, participant1, participant2) {
  72.  
  73.  
  74. let namesOfPart = this.listOfParticipants.map(obj => obj.name);
  75.  
  76. let part1Con = '';
  77.  
  78. let part2Con = '';
  79.  
  80. let powerPrPar1 = 0;
  81.  
  82. let powerPrPar2 = 0;
  83.  
  84.  
  85. for (let el of this.listOfParticipants) {
  86.  
  87. if (el.name === participant1) {
  88.  
  89. part1Con = el.condition;
  90.  
  91. powerPrPar1 = el.power;
  92.  
  93.  
  94.  
  95. } else {
  96.  
  97. part2Con = el.condition;
  98.  
  99. powerPrPar2 = el.power;
  100.  
  101.  
  102.  
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109. if (typeOfGame === 'WaterBalloonFights') {
  110.  
  111. if (!namesOfPart.includes(participant1) || !namesOfPart.includes(participant2)) {
  112.  
  113. throw new Error(`Invalid entered name/s.`);
  114.  
  115.  
  116.  
  117. } else if (part1Con !== part2Con) {
  118.  
  119. throw new Error(`Choose players with equal condition.`);
  120.  
  121.  
  122.  
  123. }
  124.  
  125.  
  126.  
  127. if (powerPrPar1 > powerPrPar2) {
  128.  
  129. this.listOfParticipants.forEach(obj => obj.name === participant1 ? obj.wins++ : false);
  130.  
  131. return `The ${participant1} is winner in the game ${typeOfGame}.`
  132.  
  133. } else if (powerPrPar1 < powerPrPar2) {
  134.  
  135. this.listOfParticipants.forEach(obj => obj.name === participant2 ? obj.wins++ : false);
  136.  
  137. return `The ${participant2} is winner in the game ${typeOfGame}.`
  138.  
  139. } else {
  140.  
  141. return `There is no winner.`;
  142.  
  143. }
  144.  
  145.  
  146.  
  147. } else if (typeOfGame === 'Battleship') {
  148.  
  149. if (!namesOfPart.includes(participant1)) {
  150.  
  151. throw new Error(`Invalid entered name/s.`);
  152.  
  153.  
  154.  
  155. }
  156.  
  157. this.listOfParticipants.forEach(obj => obj.name === participant1 ? obj.power += 20 : false);
  158.  
  159. return `The ${participant1} successfully completed the game ${typeOfGame}.`;
  160.  
  161. }
  162.  
  163. }
  164.  
  165.  
  166. toString() {
  167.  
  168. let arr = this.listOfParticipants;
  169.  
  170. let numberOfParticipants = arr.length;
  171.  
  172. let resultArr = [`${this.organizer} will take ${numberOfParticipants} participants on camping to ${this.location}`];
  173.  
  174. arr.sort((a, b) => b.wins - a.wins);
  175.  
  176. for (let obj of arr) {
  177.  
  178. let cur = `${obj.name} - ${obj.condition} - ${obj.power} - ${obj.wins}`;
  179.  
  180. resultArr.push(cur);
  181.  
  182. }
  183.  
  184. return resultArr.join('\n');
  185.  
  186. }
  187.  
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement