Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  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(!this.kids[grade]){
  11.  
  12. this.kids[grade]=[];
  13. }
  14.  
  15. if(budget<this.budget){
  16. return `${name}'s money is not enough to go on vacation to ${this.destination}.`
  17. }
  18.  
  19. let exists=false;
  20. this.kids[grade].forEach(element => {
  21. let currName = element.split('-')[0];
  22. if (currName === name) {
  23. exists=true;
  24. }
  25. });
  26.  
  27. if(exists){
  28. return `${name} is already in the list for this ${this.destination} vacation.`
  29.  
  30. }
  31.  
  32. this.kids[grade].push(`${name}-${budget}`);
  33.  
  34. return this.kids[grade];
  35. }
  36.  
  37. removeChild(name,grade){
  38. let foundKid=this.kids[grade].find(curr=>curr.split('-')[0]===name);
  39. if(foundKid){
  40. let ind=this.kids[grade].indexOf(foundKid);
  41. this.kids[grade].splice(ind,1);
  42. return this.kids[grade];
  43. }
  44. else{
  45. return `We couldn't find ${name} in ${grade} grade.`;
  46. }
  47. }
  48.  
  49. toString(){
  50. Object.entries(this.kids).sort((a,b)=>{
  51. a[0]-b[0];
  52. })
  53. let result='';
  54. let num=0;
  55. Object.values(this.kids).forEach(arr=>{
  56. arr.forEach(kid=>num++);
  57. });
  58.  
  59. result+= `${this.organizer} will take ${num} children on trip to ${this.destination}`
  60.  
  61. Object.entries(this.kids).forEach(gradeKVP=>{
  62. let seq=0;
  63. if(gradeKVP[1].length>0){
  64. result+= `\nGrade: ${gradeKVP[0]}`;
  65. gradeKVP[1].forEach(kidLine=>{
  66. let ind= gradeKVP[1].indexOf(kidLine);
  67. result+= `\n${++seq}. ${kidLine}`;
  68. // if(ind<gradeKVP[1].length-1){
  69.  
  70. // }
  71. // else{
  72. // result+= `${++seq}. ${kidLine}`;
  73. // }
  74. })
  75. result+= '\n';
  76. }
  77. })
  78. return `${result}`;
  79. }
  80. }
  81.  
  82. let vacation = new Vacation('Miss Elizabeth', 'Dubai', 2000);
  83. vacation.registerChild('Gosho', 5, 3000);
  84. vacation.registerChild('Lilly', 6, 1500);
  85. vacation.registerChild('Pesho', 7, 4000);
  86. vacation.registerChild('Tanya', 5, 5000);
  87. vacation.registerChild('Mitko', 10, 5500)
  88. console.log(vacation.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement