Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Vacation {
- constructor(organizer, destination, budget) {
- this.organizer = organizer;
- this.destination = destination;
- this.budget = budget;
- this.kids = {};
- }
- get numberOfChildren() {
- //problem description changed during exam - new return value;
- let sumChildren = 0;
- //let empty = `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`
- Object.values(this.kids).forEach( gr => {sumChildren += gr.length});
- //return sumChildren > 0 ? sumChildren : empty;
- return sumChildren;
- }
- registerChild(name, grade, budget){
- let kid = `${name}-${budget}`;
- if(budget < this.budget) {
- return `${name}\'s money is not enough to go on vacation to ${this.destination}.`;
- } else {
- if(this.kids.hasOwnProperty(grade)) {
- if(this.kids[grade].includes(kid)) {
- return `${name} is already in the list for this ${this.destination} vacation.`
- } else {
- this.kids[grade].push(kid);
- return this.kids[grade];
- }
- } else {
- this.kids[grade] = [];
- this.kids[grade].push(kid);
- return this.kids[grade];
- }
- }
- }
- removeChild(name, grade) {
- if(this.kids.hasOwnProperty(grade)) {
- let gradeToSearch = this.kids[grade];
- for (let i = 0; i < gradeToSearch.length; i++) {
- if(gradeToSearch[i].startsWith(name)) {
- gradeToSearch.splice(i, 1);
- return gradeToSearch;
- }
- }
- }
- return `We couldn\'t find ${name} in ${grade} grade.`
- }
- toString() {
- let output = '';
- output += `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
- if(this.numberOfChildren > 1) {
- output = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`
- for (const gr of Object.keys(this.kids)) {
- output += `Grade: ${gr}\n`;
- let count = 1;
- for (const kid of this.kids[gr]) {
- output += `${count++}. ${kid}\n`;
- }
- // output += '\n'
- //- incorrect problem description, judge zero tests failed;
- }
- }
- return output;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement