JavaScript Object-Oriented JavaScript Getters and Setters Creating Setter Methods

Mary Pienzi
Mary Pienzi
Full Stack JavaScript Techdegree Student 6,994 Points

Why is my setter method returning the wrong value? I'm checking the student level.

If the student.level is Senior or Junior then I set the "this._major" to equal major. If the student.level is not then "this._major" defaults to "None".

creating_setters.js
class Student {
    constructor(gpa, credits){
        this.gpa = gpa;
        this.credits = credits;
    }

    stringGPA() {
        return this.gpa.toString();
    }

  set major(major){
    if(student.level === "Senior" || student.level === "Junior"){ 
      this._major = major;
    }else{
      this._major = "None";
    }
  } 

    get level() {
        if (this.credits > 90 ) {
            return 'Senior';
        } else if (this.credits > 60) {
            return 'Junior';
        } else if (this.credits > 30) {
            return 'Sophomore';
        } else {
            return 'Freshman';
        }
    }
}

var student = new Student(3.9, 60);

2 Answers

Brandon White
seal-mask
MOD
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Treehouse Moderator

Hi Mary Pienzi,

Great job getting started.

So the issue you're running into is actually two-fold:

  1. In the conditional within your setter method, you're checking whether student.level is strictly equal to "Senior" or "Junior", but student is undefined. What you're wanting to check is if this.level is strictly equal to "Senior" or "Junior".
  2. The other thing is (behind the scenes, the challenge is calling major on the student variable (which is an instance of the Student class) to retrieve the value of the student's major. But the student's major is actually stored in the property _major. So you'll want to add a getter method for major (and within that function you'll want to return this._major. That way when student.major is called the correct value is returned.
Mary Pienzi
Mary Pienzi
Full Stack JavaScript Techdegree Student 6,994 Points

Thanks. I was close. It was all in my notes. I should read my own notes. Not your first statement though. I did question whether student.level was the correct parameter I needed. That is now in my notes.