Advertisement
Danny_Berova

03.Vacation

Nov 18th, 2018
181
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.     get numberOfChildren() {
  10.         //problem description changed during exam - new return value;
  11.         let sumChildren = 0;
  12.         //let empty = `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`
  13.  
  14.         Object.values(this.kids).forEach( gr => {sumChildren += gr.length});
  15.  
  16.         //return sumChildren > 0 ? sumChildren : empty;
  17.         return sumChildren;
  18.     }
  19.  
  20.     registerChild(name, grade, budget){
  21.         let kid = `${name}-${budget}`;
  22.         if(budget < this.budget) {
  23.             return `${name}\'s money is not enough to go on vacation to ${this.destination}.`;
  24.        } else {
  25.            if(this.kids.hasOwnProperty(grade)) {
  26.                if(this.kids[grade].includes(kid)) {
  27.                    return `${name} is already in the list for this ${this.destination} vacation.`
  28.                } else {
  29.                    this.kids[grade].push(kid);
  30.                    return this.kids[grade];
  31.                }
  32.            } else {
  33.                this.kids[grade] = [];
  34.                this.kids[grade].push(kid);
  35.                return this.kids[grade];
  36.            }
  37.        }
  38.    }
  39.  
  40.    removeChild(name, grade) {
  41.        if(this.kids.hasOwnProperty(grade)) {
  42.            let gradeToSearch = this.kids[grade];
  43.  
  44.            for (let i = 0; i < gradeToSearch.length; i++) {
  45.                if(gradeToSearch[i].startsWith(name)) {
  46.                    gradeToSearch.splice(i, 1);
  47.                    return gradeToSearch;
  48.                }
  49.            }
  50.        }
  51.        return `We couldn\'t find ${name} in ${grade} grade.`
  52.    }
  53.  
  54.    toString() {
  55.        let output = '';
  56.        output += `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
  57.        
  58.        if(this.numberOfChildren > 1) {
  59.            output = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`
  60.  
  61.            for (const gr of Object.keys(this.kids)) {
  62.                output += `Grade: ${gr}\n`;
  63.                let count = 1;
  64.                for (const kid of this.kids[gr]) {
  65.                    output += `${count++}. ${kid}\n`;
  66.                }
  67.            // output += '\n'
  68.            //- incorrect problem description, judge zero tests failed;
  69.            }
  70.        }
  71.        return output;
  72.    }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement