"Java Objects (Retired)" was retired on January 31, 2017. You are now viewing the recommended replacement.

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 Davis
Michael Davis
6,992 Points

Can someone help me understand what I'm doing wrong

I'm trying to figure out why my code isn't working. In my mind it makes sense but it says "unexpected identifier"

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() {
   return this._major
  }

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

var student = new Student(3.9, 60);

1 Answer

Steven Parker
Steven Parker
231,275 Points

The braces are unbalanced, there seems to be a missing closing brace for the method.

A few extra hints:

  • check the comparisons and logic to be sure they are doing what you want
  • setters don't need to return anything
  • you don't need to create a getter for this challenge
Michael Davis
Michael Davis
6,992 Points

I'm still confused. Are you saying it should look more like this?

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

Steven Parker
Steven Parker
231,275 Points

Instead of an empty block with an "else", you could just have a normal "if" where comparisons (and logic) check for the condition(s) where you want to reassign the backing variable.