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

Nick White
Nick White
8,326 Points

Challenge Task 2 of 2

"Inside the major() setter method, set the student's major to a backing property "major". [underlined] If the student's level is Junior or Senior, the value of the backing property should be equal to the parameter passed to the setter method. If the student is only a Freshman or Sophomore, set the [/underlined] "major" backing property equal to 'None'."

I don't know what it wants me to do. The statement is so vague and weirdly put that it may as well be written in Aramaic for all I know. It tells me to set up a "backing property", a term the lesson has never mentioned since it was mentioned here. Bizarre nomenclature aside I assume it wants me to make a property with the "this" keyword and so I do, but then it says it doesn't accept it because the call stack size has exceeded the maximum and I... I just don't know what to do about the underlined part.

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

    stringGPA() {
    }
    set major(major) {
         this.major = major;
    }
    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

Balazs Peak
Balazs Peak
46,160 Points

There is two major things I'd point out:

  • The method should only set the given value to the objects' property if the object has the required level. This is a logical part which you should take the effort to understand.
  • The another thing is that for some reason, the Treehouse Code Challenge Platform only accepts the solution if the backing property is started with an underscore. (At least that's what has happened to me.) I highly doubt that this has anything to do with JavaScript syntax, I'd rather say that it is a convention based criteria. Further more, I wouldn't think this should be in the Code Challenge requirements to be able to pass the challenge. That being said, I don't think you should take much effort to understand it, if it is not clear.

Sometimes programmers use underscore as a first character of variable names so that they can distinguish them from similar variables. In this case, the object property could have been mistaken with the parameter passed in. As you can see, I've used a plain "m" for the parameter variable name, so it could not have been mistaken anyway. (But I still couldn't pass the challenge without the underscore, which is really annoying.)

    set major(m){

      if(this.level === "Senior" || this.level === "Junior") 
      {
        this._major = m;
      }
      else if(this.level === "Sophomore" || this.level === "Freshman")
      {
        this._major = "None";
      }

    }