Advertisement
Todorov_Stanimir

02. Vacation JS Advanced Exam Preparation - July 2019

Oct 20th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Vacation {
  2.     //correct constructor is test 1
  3.     constructor(organizer, destination, budget) {
  4.         this.organizer = organizer;
  5.         this.destination = destination;
  6.         this.budget = budget;
  7.         this.kids = {};
  8.     }
  9.  
  10.     get numberOfChildren() { return Object.values(this.kids).reduce((a, b) => a + b.length, 0) }
  11.  
  12.     budgetIsNotEnought(budget) { return (budget < this.budget) ? true : false };
  13.  
  14.     childrenExist(grade, name) { return this.kids[grade].findIndex(k => k.startsWith(name)) !== -1 ? true : false };
  15.  
  16.     gradeExist(grade) { return this.kids[grade] ? true : false };
  17.  
  18.     indexOfChildren(grade, name) { return this.kids[grade].findIndex(k => k.startsWith(name)) }
  19.  
  20.     registerChild(name, grade, budget) {
  21.         //test 2
  22.         if (this.budgetIsNotEnought(budget) === true) {
  23.             return `${name}'s money is not enough to go on vacation to ${this.destination}.`
  24.        }
  25.        //test 3
  26.        if (this.gradeExist(grade) === true && this.childrenExist(grade, name) === true) {
  27.            return `${name} is already in the list for this ${this.destination} vacation.`
  28.        }
  29.        //test 4 and 5
  30.        if (this.gradeExist(grade) === false) {
  31.            this.kids[grade] = [];
  32.        }
  33.  
  34.        this.kids[grade].push(`${name}-${budget}`)
  35.        return this.kids[grade];
  36.    }
  37.  
  38.    removeChild(name, grade) {
  39.        //test 6
  40.        if (this.gradeExist(grade) == false || this.childrenExist(grade, name) == false) {
  41.            return `We couldn't find ${name} in ${grade} grade.`
  42.         }
  43.         //test 7
  44.         this.kids[grade].splice(this.indexOfChildren(grade, name), 1);
  45.         return this.kids[grade];
  46.     }
  47.  
  48.     toString() {
  49.         //test 9
  50.         if (this.numberOfChildren === 0) {
  51.             return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`
  52.         }
  53.         //test 8
  54.         let result = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
  55.         Object.entries(this.kids).map(grade => {
  56.             result += `Grade: ${grade[0]}\n`;
  57.             grade[1].map((kid, ind) => result += `${ind + 1}. ${kid}\n`);
  58.         });
  59.  
  60.         return result;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement