Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 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(budget < this.budget){
  11. return `${name}'s money is not enough to go on vacation to ${this.destination}.`
  12. }
  13. if(this.kids.hasOwnProperty(grade)){
  14. for (const kid of this.kids[grade]) {
  15. if (kid === `${name}-${budget}`) {
  16. return `${name} is already in the list for this ${this.destination} vacation.`
  17. }
  18. }
  19. this.kids[grade].push(`${name}-${budget}`);
  20. }else{
  21. this.kids[grade] = [];
  22. this.kids[grade].push(`${name}-${budget}`);
  23. }
  24. return this.kids[grade];
  25. }
  26.  
  27. removeChild(name,grade){
  28. if(this.kids.hasOwnProperty(grade)){
  29. for (const kid of this.kids[grade]) {
  30. let kidInfo = kid.split('-');
  31. let kidName = kidInfo[0];
  32. if(kidName === name){
  33. let index = this.kids[grade].indexOf(kid);
  34. this.kids[grade].splice(index,1);
  35. return this.kids[grade];
  36. }
  37. }
  38. }else{
  39. return `'We couldn't find ${name} in ${grade} grade.`
  40. }
  41. }
  42.  
  43. toString(){
  44. if(this.numberOfChildren === 0){
  45. return `No children are enrolled for the trip and the organization of ${this.organizer} falls out...`;
  46. }
  47. let result = `${this.organizer} will take ${this.numberOfChildren} children on trip to ${this.destination}\n`;
  48.  
  49. Object.entries(this.kids).sort((a,b)=>a[0]-b[0]);
  50.  
  51. for (const grade in this.kids) {
  52. result+=`Grade: ${grade}\n`;
  53. let currentNumber = 1;
  54.  
  55. for (const kid of this.kids[grade]) {
  56. result+=`${currentNumber}. ${kid}\n`;
  57. currentNumber++;
  58. }
  59. }
  60. return result;
  61. }
  62.  
  63. get numberOfChildren(){
  64. this._numberOfChildren = 0;
  65. for (const grade in this.kids) {
  66. this._numberOfChildren+=this.kids[grade].length;
  67. }
  68. return this._numberOfChildren;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement