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

Dehn Hunsworth
Dehn Hunsworth
7,189 Points

Not getting correct return from a setter method..

Been at this one a while and not sure where I am going wrong here. Tried to reference the video before this challenge and still not able to see what I am missing. Any hints or explanations much appreciated.

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

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

    get major() {
        return this._major;
    }

    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);

1 Answer

Emmanuel C
Emmanuel C
10,636 Points

Hey Dehn,

It seems like youre trying to access the student object in the setter, but there is no student object in that scope. I believe you want to check the current student object that is calling the setter method. So youll need to use the "this" keyword. Replacing student.level with this.level will fix this.

Dehn Hunsworth
Dehn Hunsworth
7,189 Points

Thanks! that was it. still new to 'this' so not always the first thing that pops in my head.

Thank you!