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={};
- }
- registerChild(name, grade, budget){
- if(!this.kids[grade]){
- this.kids[grade]=[];
- }
- if(budget<this.budget){
- return `${name}'s money is not enough to go on vacation to ${this.destination}.`
- }
- let exists=false;
- this.kids[grade].forEach(element => {
- let currName = element.split('-')[0];
- if (currName === name) {
- exists=true;
- }
- });
- if(exists){
- return `${name} is already in the list for this ${this.destination} vacation.`
- }
- this.kids[grade].push(`${name}-${budget}`);
- return this.kids[grade];
- }
- removeChild(name,grade){
- let foundKid=this.kids[grade].find(curr=>curr.split('-')[0]===name);
- if(foundKid){
- let ind=this.kids[grade].indexOf(foundKid);
- this.kids[grade].splice(ind,1);
- return this.kids[grade];
- }
- else{
- return `We couldn't find ${name} in ${grade} grade.`;
- }
- }
- toString(){
- Object.entries(this.kids).sort((a,b)=>{
- a[0]-b[0];
- })
- let result='';
- let num=0;
- Object.values(this.kids).forEach(arr=>{
- arr.forEach(kid=>num++);
- });
- result+= `${this.organizer} will take ${num} children on trip to ${this.destination}`
- Object.entries(this.kids).forEach(gradeKVP=>{
- let seq=0;
- if(gradeKVP[1].length>0){
- result+= `\nGrade: ${gradeKVP[0]}`;
- gradeKVP[1].forEach(kidLine=>{
- let ind= gradeKVP[1].indexOf(kidLine);
- result+= `\n${++seq}. ${kidLine}`;
- // if(ind<gradeKVP[1].length-1){
- // }
- // else{
- // result+= `${++seq}. ${kidLine}`;
- // }
- })
- result+= '\n';
- }
- })
- return `${result}`;
- }
- }
- let vacation = new Vacation('Miss Elizabeth', 'Dubai', 2000);
- vacation.registerChild('Gosho', 5, 3000);
- vacation.registerChild('Lilly', 6, 1500);
- vacation.registerChild('Pesho', 7, 4000);
- vacation.registerChild('Tanya', 5, 5000);
- vacation.registerChild('Mitko', 10, 5500)
- console.log(vacation.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement