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

Miguel Sosa
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Miguel Sosa
Front End Web Development Techdegree Graduate 24,185 Points

Setter method returns undefined

This exercised asked to create a setter that shows the major of the student based on its level but, I always get undefined as the major value.

In any case, aren't setters supposed to be used when defining a new object? It seems counter intuitive to use a setter and not a getter in this case.

3 Answers

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 38,304 Points

Hi Miguel. You are super close. If Level is Junior or Senior you want to set this._major = major NOT to this.level. this.level is Junior or Senior. you want to set this._major to the major being passed in.

Steven Parker
Steven Parker
231,275 Points

Setters and getters aren't always needed, nor are they always used together. The choice to use one or both is based on the situation.

If you're having trouble with completing the challenge, we can give you help once we see your actual code.

You might want to take a look at this video about Posting a Question. And for future issues when your question refers to something you are doing in a workspace, you may also want to view this one about sharing a snapshot of your workspace.

Miguel Sosa
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Miguel Sosa
Front End Web Development Techdegree Graduate 24,185 Points

I understand, here's a snapshot of my code: https://w.trhou.se/ykgn8qo9hi

student.level returns "Sophomore" but student.major is undefined

I tried using conditionals to set the major based on student.level but something isn't working and I can't figure out what.

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