Advertisement
Liliana797979

camping - js advanced exam

Oct 23rd, 2021
235
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(!this.priceForTheCamp.hasOwnProperty(condition)){
  15.             throw new Error("Unsuccessful registration at the camp.");
  16.         }
  17.         if(this.listOfParticipants.length > 0){
  18.             for(let currName of this.listOfParticipants){
  19.                 if(currName.name == name){
  20.                     return `The ${name} is already registered at the camp.`;
  21.                 }
  22.             }
  23.         }
  24.         if(this.priceForTheCamp.hasOwnProperty(condition)){
  25.             let currPrice = this.priceForTheCamp[condition];
  26.  
  27.             if(currPrice > money){
  28.                 return `The money is not enough to pay the stay at the camp.`
  29.             }
  30.         }
  31.  
  32.         this.listOfParticipants.push({name,condition, "power":100,"wins":0});
  33.         return `The ${name} was successfully registered.`
  34.     }
  35.     unregisterParticipant (name){
  36.         if(this.listOfParticipants.length > 0){
  37.             for(let x of this.listOfParticipants){
  38.                 if(x.name != name){
  39.                     throw new Error(`The ${name} is not registered in the camp.`);
  40.                 }
  41.             }
  42.             this.listOfParticipants.pop(name);
  43.             return `The ${name} removed successfully.`
  44.         }
  45.     }
  46.     timeToPlay (typeOfGame, participant1, participant2){
  47.         const currNam1 = "";
  48.         const currName2 ="";
  49.         let conditon1 = "";
  50.         let condition2 = "";
  51.         let powerOne = 0;
  52.         let powertwo = 0;
  53.         if(typeOfGame == "WaterBalloonFights"){
  54.             if(this.listOfParticipants.length > 0){
  55.                 for(let name of this.listOfParticipants){
  56.                     if(name.name == participant1 && name.name == participant2){
  57.                         currNam1 = participant1;
  58.                         currName2 = participant2;
  59.                         conditon1 = this.listOfParticipants.includes(x => x.condition == currNam1);
  60.                         condition2 = this.listOfParticipants.includes(x => x.condition == currName2);
  61.                         if(name.condition != conditon1 && name.condition == condition2){
  62.                             throw new Error(`Choose players with equal condition.`);
  63.                         }
  64.  
  65.                         powerOne = this.listOfParticipants[currNam1].power;
  66.                         powertwo = this.listOfParticipants[currName2].power;
  67.  
  68.                         if(powerOne > powertwo){
  69.                             this.listOfParticipants[currNam1].wins ++;
  70.                             return `The ${powerOne} is winner in the game ${typeOfGame}.`
  71.                         }else if(powertwo > powerOne){
  72.                             this.listOfParticipants[currName2].wins ++;
  73.                             return `The ${powertwo} is winner in the game ${typeOfGame}.`
  74.                         }else{
  75.                             return `There is no winner.`;
  76.                         }
  77.                     }else{
  78.                         throw new Error(`Invalid entered names.`);
  79.                     }
  80.                 }
  81.             }
  82.  
  83.  
  84.         }else if(typeOfGame == "Battleship"){
  85.             const currName = "";
  86.             const currPoints = 0;
  87.             if(this.listOfParticipants.length > 0){
  88.                 for(let name of this.listOfParticipants){
  89.                     if(name.name == participant1){
  90.                         this.listOfParticipants[participant1].power += 20;
  91.                         return `The ${currName} successfully completed the game ${typeOfGame}.`
  92.                     }else{
  93.                         throw new Error(`Invalid entered name.`);
  94.                     }
  95.                 }
  96.  
  97.             }
  98.         }
  99.     }
  100.     toString() {
  101.  
  102.         const result = [];
  103.         for(let x of this.listOfParticipants){
  104.             result.push(`${organizer} will take ${this.listOfParticipants.length} participants on camping to ${location}`);
  105.  
  106.         }
  107.         //this.listOfParticipants.map(x => x.sort((a.b)) => a.name - b.name)
  108.     }
  109. }
  110. const summerCamp = new SummerCamp("Jane Austen", "Pancharevo Sofia 1137, Bulgaria");
  111. console.log(summerCamp.registerParticipant("Petar Petarson", "student", 300));
  112. console.log(summerCamp.timeToPlay("Battleship", "Petar Petarson"));
  113. console.log(summerCamp.registerParticipant("Sara Dickinson", "child", 200));
  114. console.log(summerCamp.timeToPlay("WaterBalloonFights", "Petar Petarson", "Sara Dickinson"));
  115. console.log(summerCamp.registerParticipant("Dimitur Kostov", "student", 300));
  116. console.log(summerCamp.timeToPlay("WaterBalloonFights", "Petar Petarson", "Dimitur Kostov"));
  117.  
  118.  
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement