Need a little guidance in terms of right way to go about


#1

Hi, so if I want to log the certifications while i perform the addCertification function like so- console.log(nurseOlynyk.addCertification(‘Genetics’));

Can i add a return statement to the addCertification function after a new certification gets added to the array. Or would that be the wrong way to go about doing it.

Lecture link- https://www.codecademy.com/courses/learn-javascript-classes/lessons/classes/exercises/inheritance-v?action=resume_content_item&course_redirect=introduction-to-javascript

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;
  } 
  get certifications() {
    return this._certifications;
  }
  addCertification(newCertification) {
    this.certifications.push(newCertification);
  }
}

const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
nurseOlynyk.takeVacationDays(5);
console.log(nurseOlynyk.remainingVacationDays);
nurseOlynyk.addCertification('Genetics');
console.log(nurseOlynyk.certifications);

Hi, so if I want to log the certifications while i perform the addCertification function like so- console.log(nurseOlynyk.addCertification(‘Genetics’));

Can i add a return statement to the addCertification function after a new certification gets added to the array. Or would that be the wrong way to go about doing it.


#2

addCertification(newCertification) {
this.certifications.push(newCertification);
return this._certifications;
}

doing this makes the code run and print similarly although it just saves one line of code.


#3

For many things like this the answer is… it depends.

There are few things to consider. First, why are you logging to console? Is this behavior you want long term? Or is this just for debugging, testing or confirming your code?

Next, what might this function be used for in the future? How would you use this in the future as your program evolves?

It’s less important to save lines of code than it is to create code that’s easier to maintain and better suited for your overall goals.

Imagine you wrapped that add function in a loop to add many things. Would it be better if it returned just the item added, the entire list or simply nothing? Now imagine that loop is enormous… many items. Does it change your answer?

If you had no reason other than debugging to change the behavior of that function, I’d stick with your original approach.


#4

thanks for speedy reply, so basically it will get clearer going forward as i code which way to go about doing


#5

More or less.

You’ll learn from experience.

At the moment, while you’re learning a programming language, you’re focus probably should be more on what can be done. As such, just doing your task two ways was a very good exercise. The more options you’re aware of, the better you’ll be at choosing which most suits your needs.

Pay attention as you change your code for larger projects. What choices in the past tend to cause you more work or greater risk of error in the future?