Classes Inheritance IV / nurseOlynyk

Hi,

I’m just not sure why I get “undefined” when I call nurseOlynyk.takeVacationDays(5). I expected to see 5.
When I call nurseOlynyk.remainingVacationDays, I get 15, what is exactly what I expected.
Can someone please explain why in the first part the result is"undefined"? Should it be like that or have I done sth wrong?

My code:

class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  get name() {
    return this._name;
  }
  
  get remainingVacationDays() {
    return this._remainingVacationDays;
  }
  
  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
  }
}

class Nurse extends HospitalEmployee {
 constructor(name, certifications) {
   super(name);
   this._certifications = certifications;
 } 
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);

console.log(nurseOlynyk.takeVacationDays(5));
console.log(nurseOlynyk.remainingVacationDays);

takeVacationDays method doesn’t have a return value. You could return the remaining vacation days, that might be convenient:

  takeVacationDays(daysOff) {
    this._remainingVacationDays -= daysOff;
    return remainingVacationDays;
  }
1 Like