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

There isn't anything in the videocourse so far that shows us how to answer question 2 is there?

Isn't this a jump?

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

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

    set major(_major) {
      if(this.credits > 60){
        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

So it worked when I made the following changes, but I would still like to know whyyy:

set major(major) { if(this.credits > 60){ this._major = major

The video just before the challenge is called "Setters". This topic is covered starting at about one minute into the video. To answer your question, you use the underscore because the name of a property cannot be the same as the name of a getter or setter method. And typically, the underscore is going to be in the this portion of the assignment, like so:

set major(major) {
  if (this.credits > 60) { this._major = major }
  else { this._major = 'None'; }
}