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

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.