FAQ: Classes - Method Calls

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Method Calls” exercise from the lesson “Classes”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Introduction To JavaScript

FAQs on the exercise Method Calls

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

.takeVacationDays() isn’t a method of Surgeon, so I don’t get why we’re calling it.

As I understand it, ‘.takeVacationDays()’ is a method that we are calling on the ‘instance’ of ‘surgeonCurry’ (in this exercise).

Can anyone else weight in and let me know if I am understanding that correctly?

1 Like

Yes @runjaj2 you are correct. It is a method called on the ‘instance’ of ‘surgeonCurry’ to find out how many days he has left.

@byteblitz the .takeVacationDays() is actually a method of the Surgeon class

4 Likes

Oh, OK. Thanks for the help (you and runjaj2)!

As guided in the exercise, I did this,

console.log(surgeonCurry.name);
console.log(surgeonCurry.remainingVacationDays);

I don’t understand how and from where are we able to access name & department properties when in class we have used _name & _department as property names.

Please help me understand if i have any concept gap in between. TIA :slightly_smiling_face:

thanks to the getters, surgeonCurry.name will use the getter.

3 Likes

Oh now its clear, thank you @stetim94 :grin:

Hello, just wondering why console.log(surgeonCurry._remainingVacationDays) doesn’t print 20 instead of 17, since in the preceding step we took 3 days off of the .remainingVacationDays (the getter)? I thought using a getter was supposed to be a way of preserving the original Object?

Thanks!

No, that is not what getters are for. Getters allow us to change the behavior of a property. For example, a property could have an array value, but we could use getter to return a string instead.

1 Like

thanks i get it now!

I added some setters to the code for this section and for some reason now when the takeVacationDays method does not seem to change the value of _remainingVacationDays:

class Surgeon {
  constructor(name, department) {
    this._name = name;
    this._department = department;
    this._remainingVacationDays = 20;
  }
  get name(){
    if (typeof this._name === "string"){
      return this._name;
    } else{
      console.log("Error: could not retrieve name")
    }
  }
  set name (newName){
    if (typeof newName === "string"){
      this._name = newName;
    }else{
      console.log("Error: new name must be a string");
    }
  }
  get department(){
    if (typeof this._department === "string"){
      return this._department;
    } else{
      console.log("Error: could not retrieve department")
    }
  }
    set department (newDepartment){
    if (typeof newDepartment === "string"){
      this._department = newDepartment;
    }else{
      console.log("Error: new department must be a string");
    }
  }
   get remainingVacationDays(){
    if (typeof this._remainingVacationDays === "number"){
      return this._remainingVacationDays;
    } else{
      console.log("Error: could not retrieve number of remaining vacation days")
    }
  }
    set remainingVacationDays (newNumber){
    if (typeof newNumber === "number"){
      this._name = newNumber;
    }else{
      console.log("Error: new number of vacation days must be a number");
    }
  }
  takeVacationDays(daysOff){
    if (typeof daysOff === "number"){
      this.remainingVacationDays = this.remainingVacationDays - daysOff; 
    }else{
      console.log("Error: number of days off must be a number");
    }
  }
}

const surgeonCurry = new Surgeon('Curry', 'Cardiovascular');
const surgeonDurant = new Surgeon('Durant', 'Orthopedics');

console.log(surgeonCurry.name)
surgeonCurry.takeVacationDays(3)
console.log(surgeonCurry.remainingVacationDays)

This code outputs the following:

Curry
20

but I expected it to output:

Curry
17

What’s going on?

Lets go through the code:

this.remainingVacationDays = this.remainingVacationDays - daysOff; 

this looks fine. (you can log if you want). So where does this take us? assignment will trigger the setter:

    set remainingVacationDays (newNumber){
    if (typeof newNumber === "number"){
      this._name = newNumber;
    }else{
      console.log("Error: new number of vacation days must be a number");
    }
  }

it seems the setter doesn’t update the right property

Thanks for your response! Why is it updating the wrong property?

Look at the code in your setter, what does the code tell you? What is the setter suppose to do? What property should be updated?

ah X( I forgot to change the name of the variable when I copy and pasted the function. Thanks!