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

Reagan Ganancial
Reagan Ganancial
9,476 Points

create setter

Maximum call stack size exceeded.. I don't get it..

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';
        }
    }
  set major(major){ 
    var student = new Student(3.9, 60);
    if (student >= 60 && student <= 90) {
        this.major = 'major';
    } else {
      this.major = 'None';
    }    
  }

}

var student = new Student(3.9, 60);

3 Answers

Matthew Lang
Matthew Lang
13,483 Points

Read the question carefully. A 'backing' property is generally a property that is never accessed directly, but by getters and setters. Backing properties usually start with an _ (underscore) at the beginning of their name, as so to differ them from the getter and setter method names.

The correct code is as follows:

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

We create a setter method, by using the set keyword. We pass in 'major' as our argument, which will store the type of major. We then check to see if the current level of the user is Junior or Senior, as this is what the question asks us to do. Here we use the level getter method to calculate and return the value. If the user is Junior or Senior, we set the backing property _major to be equal to the major argument. If they are neither, we then set it as 'None', which the question asks us to do.

If you're still struggling with getters and setters, I suggest re-watching some previous videos for revision. Don't worry, these are difficult concepts to wrap your head around first time around. You can also check the documentation on getters and setters.

Reagan Ganancial
Reagan Ganancial
9,476 Points

I would like to know that if you had your web developer job or are you still looking for it.

Matthew Lang
Matthew Lang
13,483 Points

About to start attending college. No jobs.

Reagan Ganancial
Reagan Ganancial
9,476 Points

Good luck in your studies and after my javascript fullstack course i'll eventually go for database sql and ReactJS framework. Thank you very much!

Reagan Ganancial
Reagan Ganancial
9,476 Points

Great for you! I'm taking treehouse course for fulltime and i'm a college dropout. I've seen your profile and your taking treehouse for almost one year. I'am in my fourth month and I'am asking my self, if i'am placing myself in huge risk after this course of being still unemployed. Do you think being in treehouse courses really prepare us in real job situation.

Matthew Lang
Matthew Lang
13,483 Points

I'm 16. I've no clue. I've never worked in a real job environment, though I'd say Treehouse definitely helps you get the basics/intermediates down. I advise you not to use Treehouse only, though. You should definitely use other resources, such as other site tutorials, and even looking over other people's projects and work.