Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript Getters and Setters Creating Setter Methods

Mary Pienzi
seal-mask
.a{fill-rule:evenodd;}techdegree
Mary Pienzi
Full Stack JavaScript Techdegree Student 7,036 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
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 Points

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
seal-mask
.a{fill-rule:evenodd;}techdegree
Mary Pienzi
Full Stack JavaScript Techdegree Student 7,036 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.