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

Invalid left-hand side in assignment

whats wrong?

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';
        }
    }
 get major (){
 if (this.level='Senior' || this.level='Junior'){
  return  this._major;}
   else {
   this._major = 'None'; 
   return this._major ;
   }
  }
 set major(major){
    this._major=major;
 }
}
var student = new Student(3.9, 60);

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi again! Now that you've fixed the syntax error let's take a crack at the logic error here. All this logic should go in the setter function, but you have it in the getter. A getter returns a value. A setter does not. So you will not need a return statement in the setter function. If the student is a Senior or Junior, you should be setting the major to the major variable that was passed in to the setter, otherwise set it to None.

I think you can get it with this hint, but let me know if you're still stuck! :sparkles:

lol I want credit for solving the prob before I saw your hint. But thank you for taking the time to help.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing well, but you have a bit of an error which is borderline a typo in your comparison. In fact, it's not a comparison but rather an assignment as you've accidentally used the single equals instead of the double equals signs.

You wrote:

if (this.level='Senior' || this.level='Junior'){

But you meant to write:

if (this.level=='Senior' || this.level=='Junior'){

Hope this helps! :sparkles:

Thanks that helped a lot and I was able to solve it. Turns out I didn't need the getter method. Just the setter and conditional statement.