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

Yosif Qassim
Yosif Qassim
6,367 Points

i cant understand this question

i cant understand this :

Inside the major() setter method, set the student's major to a backing property "major". 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'.

can anybody explain it to me please

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(method){
    if (this.level.Senior || this.level.Junior){
      this._method = method

    }else{}
  }
}

var student = new Student(3.9, 60);

4 Answers

Brian Jensen
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree seal-36
Brian Jensen
Treehouse Staff

Yosif,

You were on the right track logically. However, as Steven Parker mentioned: The conditionals you created in the level() getter method returns a plain string value and not an object value that can be accessed via the dot notation.

So the conditional in your major() setter method would have to look something like this:

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

:star: If you would like to get a better view of the major() setter method in action add this getter method below the setter so the _major backing property can be returned.

  get major() {
    return this._major;
  }

Then add this outside of the Student class:

var student = new Student(3.9, 61);
student.major = 'Computer Science';
console.log(student);
Sean Flanagan
Sean Flanagan
33,235 Points

Thanks Brian. That's a big help!

Steven Parker
Steven Parker
231,236 Points

It looks like you have the right idea, here's a few hints:

  • the instructions ask for a backing property named "major", but this code has "method" instead
  • the "level" property of "Student" returns a string, not an object with other properties
  • the "else" condition needs to "set the "major" backing property equal to 'None'."
Darren Ward
Darren Ward
12,025 Points

I couldn't understand this question either. I find this is generally the case with the tasks posed by this tutor.

Timothy Sawyer
Timothy Sawyer
31,052 Points

This worked for me: I set the backing property first, and then checked the value, setting it to 'None' if it isn't strictly equal to either 'Senior' or 'Junior'

     set major(major) {
        this._major = major;
        if (this.level !== 'Senior' && this.level !== 'Junior') {
            this._major = 'None';
        }
     }
Steven Parker
Steven Parker
231,236 Points

That certainly works, but it's not quite as efficient since it performs the assignment twice in some cases.