Just wanted some clarification about arrays within classes


#1
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 = 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); // 3


The exercise itself caused me no problems. I would just like some clarification on the output of the console, I don’t quite understand whats going on.

On the last line I expected the console to output the entire array like it usually does when console.log(myArray) is used. Instead it output the number of elements in the array - 3. Why would this happen? And how could I output each element of the array individually? I tried testing it with a .forEach method but that brought up an error so I’m not sure what approach to take there. Can someone point me in the right direction?


#2

If it prints 3, maybe the value is 3.


#3

Thanks for your reply. Maybe the value is 3, but I don’t know why it would be, there’s nothing in the code that assigns the value of 3 to anything. I just assumed that the 3 was referring to how many elements were stored in the _certifications array.

Creates a new instance with certifications set as an array with two elements:

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

Adds a new element to the end of the array:

nurseOlynyk.addCertification('Genetics');

My only qualm is that when console.log(nurseOlynyk.certifications); is executed the output isnt the contents of the array, but the amount of elements within it.


#4

You could look at where you do assign to it and add some print there to check what that value is


#5

Getting anywhere with that?

Ctrl-F in your browser would quickly show you each place where you do something with that field, you could add prints before and after each one if you’re not completely certain (or maybe especially if you’re completely certain, maybe it’s a bad assumption)

And === can be used to check whether it’s both the same type and value as 3

And you’ve got some strong clues already, “it printed the content before” “foreach raises an error” <- supports it not being an array, is cause to look closer at it. The error messages may also hold important information, that’s what they’re there for.


#6
addCertification(newCertification){
  this._certifications = this._certifications.push(newCertification);
}

This method doesn’t work as you would expect. A call to this._certifications.push(newCertification);, which is right hand side of the assignment returns array’s size.
Initially this._certifications is an array, then when you call nurseOlynyk.addCertification('Genetics'); it becomes 3. If you called this method again, you would get an error saying TypeError: this._certifications.push is not a function. Because push is a method for array’s only and you try to call it on 3.


#7

Ok I just went back to the exercise and ran the old code and now its printing out the array. Was probably a bug with the site because I haven’t changed anything.

class HospitalEmployee {
  constructor(name) {
    this._name = name;
    this._remainingVacationDays = 20;
  }
  
  static generatePassword() {
		return Math.floor(Math.random()*10000);
  }
  
  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);
  }
}

class Doctor extends HospitalEmployee {
  constructor(name, insurance){
    super(name);
    this._insurance = insurance;
  }
  
  get insurance() {
    return this._insurance;
  }
}






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

#8

The bug is in your code. And it’s super important to know how to identify and fix mistakes because you can really only write what you can fix (Everyone makes mistakes, usually the same mistakes, but those with more experience are better at finding and fixing them)


#9

Ok thanks for your help guys. I know it’s important to know how to identify and fix mistakes, I tried a few things before making my post but felt it would be better for my learning to get a second opinion. Alot of the time the exercises will just tick off as correct even when you do something wrong.


#10

You changed it from:
this._certifications = this._certifications.push(newCertification);
to :
this.certifications.push(newCertification);

Which is still incorrect. It should be:

this._certifications.push(newCertification);

I hope that both my answer clarified everything :slight_smile:


#11

Yeah. And that really sucks. But you can always verify yourself that the thing you wrote did its job, it’s pretty important to see the effects directly, not just a green “ok”


#12

Ahhh it’s always something really small I miss lol. Cheers guys :smiley:


#13

The problem is that it looks like its doing the job. The output is now what I expected to see. Bit confusing when it does that


#14

You’ll never stop making mistakes. You gotta learn to find them. This isn’t done by staring at the code or any such magic. Gotta follow up on the information available, start finding out what happened. Testing whether that value really was 3, and then look where it came from with help of prints


#15

I’ve just realised why that still works. Its probably less efficient I know but when this.certifications.push(newCertification); it calls the getter method and corrects itself. Which is pretty cool that it naturally does that.


#16

It doesn’t correct itself. That information isn’t there. It does exactly like you tell it to!

It’s completely stupid. It’s like a metal ball dropped into a bunch of obstacles, it’ll just roll downwards. That’s how a computer behaves. We programmers just manipulate that ball


#17

Yeah I know it doesnt correct itself really, it was just an accident that it happened that way lol. I was just remarking how this probably avoids these kinds of mistakes turning into bugs.

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

when this.certifications is called it goes to the getter method which returns this._certifications. I know this wouldn’t always happen, but I thought it was kind of neat :grin:


#18

this.certifications returns this._certifications and then the push works as expected. However it is very smelly code, so better change it. As you see it is all explainable. If you have a problem like this with your code, it is often useful to copy the code, open new tab in your browser, open console (ctrl+shift+i) and paste it, hit enter and see what happens. If it’s giving you same errors as Codecademy, then the problem is in the code.
I did this while helping you and I opened nurseOlynyk object and i saw that _certifications is indeed 3 which is array’s size. Then i looked when it is assigned and i saw that push method of Array object must be return its size.


#19

Yeah I did change it, cheers. it was just confusing at first because I was like “Hang on, why is it working when I haven’t made the corrections?”.

The Console trick is a useful thing to know, thanks a lot! I’ll be using that one from now on :sunglasses:


#20

Quick question, how do you open up an object in the console? Like you said you did with nurseOlynyk? Thanks