Advertisement
Liliana797979

vacation - js advanced exam

Oct 22nd, 2021
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Vacation {
  2.     constructor(organizer, destination, budget) {
  3.         this.organizer = organizer;
  4.         this.destination = destination;
  5.         this.budget = budget;
  6.         this.kids = {};
  7.     }
  8.  
  9.     registerChild(name, grade, budget) {
  10.         if (this.budget > budget) {
  11.             return `${name}'s money is not enough to go on vacation to ${this.destination}.`;
  12.        }
  13.  
  14.        if (!this.kids.hasOwnProperty(grade)) {
  15.            this.kids[grade] = [];
  16.        }
  17.  
  18.        for (let students of this.kids[grade]) {
  19.            let kidName = students.split("-")[0]
  20.            if (kidName === name) {
  21.                return `${name} is already in the list for this ${this.destination} vacation.`;
  22.            }
  23.        }
  24.  
  25.        let pattern = `${name}-${budget}`;
  26.        this.kids[grade].push(pattern);
  27.        return this.kids[grade];
  28.    }
  29.  
  30.    removeChild(name, grade) {
  31.        if (!this.kids.hasOwnProperty(grade)) {
  32.            return `We couldn't find ${name} in ${grade} grade.`
  33.         }
  34.  
  35.         let index = -1;
  36.         for (let students of this.kids[grade]) {
  37.             let kidName = students.split("-")[0];
  38.             index++;
  39.             if (kidName === name) {
  40.                 this.kids[grade].splice(index, 1);
  41.                 return this.kids[grade];
  42.             }
  43.         }
  44.  
  45.         return `We couldn't find ${name} in ${grade} grade.`
  46.    }
  47.  
  48.    toString() {
  49.        if (this.numberOfChildren === 0) {
  50.            return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
  51.        }
  52.  
  53.        let output = `${this.organizer} will take ${numberOfChildren} children on trip to ${this.destination}\n`;
  54.        this.kids = this.kids.sort((a, b) => {
  55.            return Number(Object.keys(a)) - Number(Object.keys(b));
  56.        });
  57.  
  58.        for (let key in this.kids) {
  59.            output += `Grade: ${key}\n`
  60.            let number = 1;
  61.            for (let student of this.kids[key]) {
  62.                output += `${number++}. ${student}\n`
  63.            }
  64.        }
  65.  
  66.        return output;
  67.    }
  68.  
  69.    get numberOfChildren() {
  70.        let counter = 0;
  71.        for (let grade in this.kids) {
  72.            counter += this.kids[grade].length;
  73.        }
  74.  
  75.        return counter;
  76.    }
  77. }
  78.  
  79. // let vacation = new Vacation('Miss Elizabeth', 'Dubai', 2000);
  80. // vacation.registerChild('Gosho', 5, 3000);
  81. // vacation.registerChild('Lilly', 6, 1500);
  82. // vacation.registerChild('Pesho', 7, 4000);
  83. // vacation.registerChild('Tanya', 5, 5000);
  84. // vacation.registerChild('Mitko', 10, 5500)
  85. // console.log(vacation.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement