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

Setter method is returning the wrong value for the major property, don't know how to get the right one

I'm so confused. I don't understand how get and set work properly. The if statement in the setter is being completely ignored, I tried swapping it to the getter section to no avail and now I have no idea what to do.

I'm finding this getter and setter section very confusing, and can't think of how it can be used in the real world

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

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

    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';
        }
    }

    get major() {
      return this._major;
    }

    set major(major){
      if (this.level === 'Junior' || 'Senior') {
            this._major = major;
          } else {
            this._major === 'none';
          }
    }
}

var student = new Student(3.9, 60);

1 Answer

Steven Parker
Steven Parker
231,184 Points

This challenge doesn't ask you to create a getter. The setter has two syntax issues (and a misspelling):

  • logic operators only work when combining complete comparisons
  • the assignment operator is "=", a "===" is a comparison operator
  • the challenge asks for the default setting to be "None" (with capital "N")
      if (this.level === 'Junior' || this.level === 'Senior') {  // use COMPLETE comparisons
            this._major = major;
          } else {
            this._major = 'None';                                // = instead of ===, and "None"

I added the getter because I wasn't sure if that was why it wasn't working. But I didn't notice that I hadn't done the || thing correctly. Thanks, it just takes someone else looking at something to point out a few things.