Advertisement
ilianrusev

Untitled

Jan 30th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. class SummerCamp {
  2. constructor(organizer, location) {
  3. this.organizer = organizer
  4. this.location = location
  5. this.priceForCamp = { "child": 150, "student": 300, "collegian": 500 }
  6. this.listOfParticipants = []
  7. this.namesArr = [];
  8. }
  9.  
  10. registerParticipant(name, condition, money) {
  11. if (!this.priceForCamp.hasOwnProperty(condition)) {
  12. throw new Error("Unsuccessful registration at the camp.")
  13. }
  14. this.namesArr = [];
  15. for (const obj of this.listOfParticipants) {
  16. this.namesArr.push(obj.name)
  17. }
  18. if (this.namesArr.includes(name)) {
  19. return `The ${name} is already registered at the camp.`
  20. }
  21.  
  22. if (money < this.priceForCamp[condition]) {
  23. return `The money is not enough to pay the stay at the camp.`
  24. } else {
  25. this.listOfParticipants.push({
  26. name: name,
  27. condition: condition,
  28. power: 100,
  29. wins: 0,
  30. })
  31. return `The ${name} was successfully registered.`
  32. }
  33. }
  34.  
  35. unregisterParticipant(name) {
  36. this.namesArr = [];
  37. for (const obj of this.listOfParticipants) {
  38. this.namesArr.push(obj.name)
  39.  
  40. if (obj.name == name) {
  41. let index = this.listOfParticipants.indexOf(obj)
  42. this.listOfParticipants.splice(index, 1)
  43. return `The ${name} removed successfully.`
  44. }
  45. }
  46. if (!this.namesArr.includes(name)) {
  47. throw new Error(`The ${name} is not registered in the camp.`)
  48. }
  49. }
  50.  
  51. timeToPlay(typeOfGame, participant1, participant2) {
  52. if (participant2 == undefined) {
  53. this.namesArr = [];
  54. for (const obj of this.listOfParticipants) {
  55.  
  56. this.namesArr.push(obj.name)
  57. }
  58. if (!this.namesArr.includes(participant1)) {
  59. throw new Error(`Invalid entered name/s.`)
  60. }
  61. let participant = this.listOfParticipants.find(el => el.name == participant1)
  62. participant.power += 20;
  63. return `The ${participant1} successfully completed the game ${typeOfGame}.`
  64.  
  65. } else {
  66. this.namesArr = [];
  67. for (const obj of this.listOfParticipants) {
  68.  
  69. this.namesArr.push(obj.name)
  70. }
  71. if (!this.namesArr.includes(participant1)) {
  72. throw new Error(`Invalid entered name/s.`)
  73. }
  74. if (!this.namesArr.includes(participant2)) {
  75. throw new Error(`Invalid entered name/s.`)
  76. }
  77. let first = this.listOfParticipants.find(el => el.name == participant1)
  78. let second = this.listOfParticipants.find(el => el.name == participant2)
  79.  
  80. if (first.condition != second.condition) {
  81. throw new Error(`Choose players with equal condition.`)
  82. }
  83.  
  84. if (first.power > second.power) {
  85. first.wins += 1;
  86. return `The ${participant1} is winner in the game ${typeOfGame}.`
  87. } else if (first.power < second.power) {
  88. second.wins += 1;
  89. return `The ${participant2} is winner in the game ${typeOfGame}.`
  90. } else {
  91. return `There is no winner.`
  92. }
  93. }
  94. }
  95.  
  96. toString() {
  97. let output = []
  98. let sorted = this.listOfParticipants.sort((obj1, obj2) => obj2.wins - obj1.wins)
  99. let string = `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}`
  100.  
  101. sorted.forEach(obj => {
  102. output.push(`${obj.name} - ${obj.condition} - ${obj.power} - ${obj.wins}`);
  103. })
  104. output.unshift(string)
  105. return output.join("\n")
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement