JavaScript Object-Oriented JavaScript Getters and Setters Creating Setter Methods

Lucciano Molina
Lucciano Molina
5,737 Points

Inside the major() setter method, set the student's major to a backing property "_major".

It's saying "unexpected { " in the code I can't seem to find it. Can someone review my code and give me some feedback, please. I am not completely sure that my code is even correct. Thank you.

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){
      if(this.level === "Senior" || this.level === "Junior"){
        return this._major = major
      }else (this.level === "Sophmore" || this.level === "Freshman"){
        return this._major = "None"
      }
    }
}

var student = new Student(3.9, 60);

1 Answer

Ella Ruokokoski
Ella Ruokokoski
20,851 Points

You The error message is a bit off, you should have else if instead of else inside your setter method. Also, setter does not return a value so you should remove the returns. The get major is not a method without the brackets. But that method is redundant anyway and should be removed.