Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Getters and Setters Creating Setter Methods

Frank Grillo
Frank Grillo
8,597 Points

Creating Setters

I am still having trouble creating a setter properly in this exercise. Can someone explain this a little more clearly?

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

    stringGPA() {
        return this.gpa.toString();
    }
  set major(student) {
    if(this.level === 'Senior' || this.level === 'Junior') {
      this._major = major;
      return this._major;
    } else {
      this._major = 'None'
      return this._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

Bella Bradbury
seal-mask
.a{fill-rule:evenodd;}techdegree
Bella Bradbury
Web Development Techdegree Student 12,709 Points

You're really, really close Frank! The point of this setter is to check and display a student's major. If we look at the code you've written we can see that there's a specific parameter that's being used here, major. You're going to want to pass that parameter into this setter when you're setting it up. Which means adjusting your beginning line of code to:

set major(major) {
...
}

After that, remember to add the semicolon after 'none' and you're good to go!

Steven Parker
Steven Parker
218,692 Points

I would add that setters don't need to return anything.