Advertisement
mark79

JS Applications Retake Exam - 12 December 2020 - Vacation

Dec 13th, 2020 (edited)
1,199
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 (budget < this.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.        if (this.kids[grade].some((x) => {
  19.            return (x.split('-')[0] === name);
  20.        })) {
  21.            return `${name} is already in the list for this ${this.destination} vacation.`;
  22.        } else {
  23.            this.kids[grade].push(`${name}-${budget}`);
  24.            return this.kids[grade];
  25.        }
  26.    }
  27.  
  28.    removeChild(name, grade) {
  29.        if (this.kids.hasOwnProperty(grade)) {
  30.            const index = this.kids[grade].map((x) => x.split('-')[0]).indexOf(name);
  31.            if (index >= 0) {
  32.                this.kids[grade].splice(index, 1);
  33.                return this.kids[grade];
  34.            }
  35.        }
  36.  
  37.        return `We couldn't find ${name} in ${grade} grade.`
  38.     }
  39.  
  40.     toString() {
  41.         if (this.numberOfChildren() === 0) {
  42.             return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
  43.         } else {
  44.             let result = [`${this.organizer} will take ${this.numberOfChildren()} children on trip to ${this.destination}`];
  45.  
  46.             Object.entries(this.kids)
  47.                 .sort((a, b) => {
  48.                     return Number(a[0] - Number(b[0]))
  49.                 })
  50.                 .forEach((x) => {
  51.                     result.push(`Grade: ${x[0]}`);
  52.                     x[1].reduce((acc, x, index) => {
  53.                         acc.push(`${index + 1}. ${x}`);
  54.                         return acc;
  55.                     }, result);
  56.                     result.push('');
  57.                 });
  58.  
  59.             return result.join(`\n`).trim();
  60.         }
  61.     }
  62.  
  63.     numberOfChildren() {
  64.         let countOfChildren = 0;
  65.         for (let grade in this.kids) {
  66.             if (this.kids.hasOwnProperty(grade)) {
  67.                 countOfChildren += this.kids[grade].length;
  68.             }
  69.         }
  70.         return countOfChildren;
  71.     }
  72. }
  73.  
  74. // let vacation = new Vacation('Mr Pesho', 'San diego', 2000);
  75. // console.log(vacation.registerChild('Gosho', 5, 2000));
  76. // console.log(vacation.registerChild('Lilly', 6, 2100));
  77. // console.log(vacation.registerChild('Pesho', 6, 2400));
  78. // console.log(vacation.registerChild('Gosho', 5, 2000));
  79. // console.log(vacation.registerChild('Tanya', 5, 6000));
  80. // console.log(vacation.registerChild('Mitko', 10, 1590));
  81.  
  82. // -------------------------------------------------------------
  83.  
  84. // let vacation = new Vacation('Mr Pesho', 'San diego', 2000);
  85. // vacation.registerChild('Gosho', 5, 2000);
  86. // vacation.registerChild('Lilly', 6, 2100);
  87. // console.log(vacation.removeChild('Gosho', 9));
  88. // vacation.registerChild('Pesho', 6, 2400);
  89. // vacation.registerChild('Gosho', 5, 2000);
  90. // console.log(vacation.removeChild('Lilly', 6));
  91. // console.log(vacation.registerChild('Tanya', 5, 6000))
  92.  
  93. // -------------------------------------------------------------
  94.  
  95. // let vacation = new Vacation('Miss Elizabeth', 'Dubai', 2000);
  96. // vacation.registerChild('Gosho', 5, 3000);
  97. // vacation.registerChild('Lilly', 6, 1500);
  98. // vacation.registerChild('Pesho', 7, 4000);
  99. // vacation.registerChild('Tanya', 5, 5000);
  100. // vacation.registerChild('Mitko', 10, 5500);
  101. // console.log(vacation.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement