Advertisement
Liliana797979

camping vqrno reshenie - js advanced exam

Oct 23rd, 2021
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class SummerCamp{
  2.     constructor(organizer, location){
  3.         this.organizer = organizer;
  4.         this.location = location;
  5.         this.priceForTheCamp = {
  6.             child: 150,
  7.             student: 300,
  8.             collegian: 500
  9.         };
  10.         this.listOfParticipants = [];
  11.     }
  12.  
  13.     registerParticipant (name, condition, money){
  14.         if (condition !== 'child' && condition !== 'student' && condition !== 'collegian') {
  15.             throw new Error('Unsuccessful registration at the camp.');
  16.         }
  17.         if (this.listOfParticipants.some(partic => partic.name == name)) {
  18.             return `The ${name} is already registered at the camp.`;
  19.         }
  20.  
  21.         let price = -1;
  22.  
  23.         if (condition == 'child') {
  24.             price = this.priceForTheCamp.child;
  25.         }
  26.         else if (condition == 'student') {
  27.             price = this.priceForTheCamp.student;
  28.         }
  29.         else if (condition == 'collegian') {
  30.             price = this.priceForTheCamp.collegian;
  31.         }
  32.  
  33.         if (Number(money) < price) {
  34.             return `The money is not enough to pay the stay at the camp.`;
  35.         }
  36.  
  37.         let participan = {
  38.             name: name,
  39.             condition: condition,
  40.             power: 100,
  41.             wins: 0
  42.         };
  43.  
  44.         this.listOfParticipants.push(participan);
  45.  
  46.         return  `The ${participan.name} was successfully registered.`
  47.     }
  48.  
  49.     unregisterParticipant (name){
  50.         if (!this.listOfParticipants.some(p => p.name == name)) {
  51.             throw new Error(`The ${name} is not registered in the camp.`)
  52.         }
  53.        
  54.         this.listOfParticipants = removeByAttr(this.listOfParticipants, 'name', name);
  55.        
  56.        function removeByAttr(arr, attr, value){
  57.             var i = arr.length;
  58.             while(i--){
  59.                if( arr[i]
  60.                    && arr[i].hasOwnProperty(attr)
  61.                    && (arguments.length > 2 && arr[i][attr] === value ) ){
  62.        
  63.                    arr.splice(i,1);
  64.        
  65.                }
  66.             }
  67.             return arr;
  68.         }
  69.        
  70.         return `The ${name} removed successfully.`;
  71.     }
  72.  
  73.     timeToPlay (typeOfGame, participant1, participant2){
  74.         if (typeOfGame == 'WaterBalloonFights') {
  75.            
  76.             let player1 = this.listOfParticipants.find(p => p.name == participant1);
  77.             let player2 = this.listOfParticipants.find(p => p.name == participant2);
  78.  
  79.             if (!player1 || !player2) {
  80.                 throw new Error('Invalid entered name/s.')
  81.             }
  82.  
  83.             if (player1.condition != player2.condition) {
  84.                 throw new Error('Choose players with equal condition.');
  85.             }
  86.  
  87.             if (player1.power > player2.power) {
  88.                 player1.wins += 1;
  89.                 return  `The ${player1.name} is winner in the game ${typeOfGame}.`
  90.             }
  91.             if (player1.power < player2.power) {
  92.                 player2.wins += 1;
  93.                 return  `The ${player2.name} is winner in the game ${typeOfGame}.`
  94.             }
  95.             else {
  96.                 return `There is no winner.`;
  97.             }
  98.         }  
  99.         else if(typeOfGame == 'Battleship'){
  100.             let player1 = this.listOfParticipants.find(p => p.name == participant1);
  101.  
  102.             if (!player1) {
  103.                 throw new Error('Invalid entered name/s.')
  104.             }
  105.  
  106.             player1.power += 20;
  107.  
  108.             return `The ${player1.name} successfully completed the game ${typeOfGame}.`;
  109.         }
  110.     }
  111.  
  112.     toString () {
  113.  
  114.         this.listOfParticipants.sort((a, b) => b.power - a.power);
  115.  
  116.         let result = `${this.organizer} will take ${this.listOfParticipants.length} participants on camping to ${this.location}\n`;
  117.        
  118.         for (let index = 0; index < this.listOfParticipants.length; index++) {
  119.             const player = this.listOfParticipants[index];
  120.             result += `${player.name} - ${player.condition} - ${player.power} - ${player.wins}\n`
  121.         }
  122.  
  123.         return result.trim();      
  124.     }
  125. }
  126.  
  127. const summerCamp = new SummerCamp("Jane Austen", "Pancharevo Sofia 1137, Bulgaria");
  128. console.log(summerCamp.registerParticipant("Petar Petarson", "student", 300));
  129. console.log(summerCamp.timeToPlay("Battleship", "Petar Petarson"));
  130. console.log(summerCamp.registerParticipant("Sara Dickinson", "child", 200));
  131. //console.log(summerCamp.timeToPlay("WaterBalloonFights", "Petar Petarson", "Sara Dickinson"));
  132. console.log(summerCamp.registerParticipant("Dimitur Kostov", "student", 300));
  133. console.log(summerCamp.timeToPlay("WaterBalloonFights", "Petar Petarson", "Dimitur Kostov"));
  134.  
  135. console.log(summerCamp.toString());
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement