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

Zahra Toto
Zahra Toto
6,723 Points

What am I doing wrong:

I don't quite grasp the setter concept yet, so I don't see where my code goes wrong. Any help is welcome!

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';
        }
    }

  /* If the student's level is Junior or Senior, the value of the backing property should be equal 
  to the parameter passed to the setter method. If the student is only a Freshman or Sophomore, set the "major" 
  backing property equal to 'None'.  */

  get major() {
    return this._major;
  }  

  set major(major) {
    this._major = major;

    if (major === 'Junior' || 'Senior'){
          this._major = major  
      } else if (
      major === 'Freshman' || 'Sophomore'){
          this._major = 'None'
      } 
   }
}

var student = new Student(3.9, 60);

3 Answers

Steven Parker
Steven Parker
231,269 Points

You are correct to put "this." in front of the backing property name.

But it should not be placed in front of the argument name.

Zahra Toto
Zahra Toto
6,723 Points

Thanks for your reply! I changed it, still doesn't work unfortunately =(

Steven Parker
Steven Parker
231,269 Points

It looks like you made some extra changes. I don't recall precisely what you had before but now I see some additional issues:

  • the argument is being compared with the level names now instead of "this.level"
  • the conditional expressions are incomplete, the right side of the "or" is a string instead a comparison
Zahra Toto
Zahra Toto
6,723 Points

Like this? Still doesn't seem to work

 set major(major) {
      this._major = major;

      if (major === 'Junior' || major === 'Senior' ){
          this._major = major  
      } else if (
      major === 'Freshman' || major === 'Sophomore' ){
          this._major = 'None'
      }  

    }
Steven Parker
Steven Parker
231,269 Points

Now it has complete expressions, but the argument is still being compared with level names.

Zahra Toto
Zahra Toto
6,723 Points

OH I see what you mean!! That was a stupid mistake on my end. It worked, thanks so much!