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

Michael Rushton
Michael Rushton
7,126 Points

Your setter method is returning the wrong value for the major property??

Can anyone please help me with why I'm getting the above error? I think it might be to do with needing a getter to return the value properly, but i've tried with no success. The concept of getters and setters seems to be made even more complicated by the fact that the syntax uses the same names to name certain parts, so when you write 'major', it feels almost impossible to understand to which of the parts of the setter method I'm actually referring to. Any help is greatly appreciated.

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){
    this._major = major;
      if(this.level == 'Junior' || this.level == 'Senior'){
       this._major = major;
      }else{
      major = 'None';
      }
    }

}

var student = new Student(3.9, 60);

4 Answers

Michael Rushton
Michael Rushton
7,126 Points

This has stopped me dead in m tracks. No explanation in the video, just been stuck for hours trying weird combinations and Googling, rewatched the videos over and over, no way i can get to an answer. First genuine wall ive hit o my Treehouse journey after months.

Michael Rushton
Michael Rushton
7,126 Points

I'm so sorry, I just tried it again and it doesn't work. This module is so frustrating. The error messages don't help you knwo where you went wrong and the naming of the function parts is confusing.

Michael Rushton
Michael Rushton
7,126 Points

I got it in the end, i think the answer was something like this....

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

basically i think i was setting the fallback property before id done the "if" statement. I think setting this.major = major; inside the "if" statement was the way to do it, otherwise we're setting it, then checking if straight after, rather than letting the function check what is in the actual argument (which is major).

I really hope this is the right answer. I think this would have been better if every part wasn't named "major", it gets really confusing. I watched a video on youTube about it my a guy called Mosh I think and he explained it really well.

Good luck Clint!