JavaScript Object-Oriented JavaScript Getters and Setters Creating Setter Methods

Emily Luebbering
Emily Luebbering
8,305 Points

When I submit this answer I get, 'Level is not defined'

Do I need an extra line of code above my setter method defining level, or is there something wrong with my set code?

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(major){
      if (level === 'Senior' || level === 'Junior'){
      this._major = major;
      } else {
      this._major = 'None'; 
      }
    }


    }


var student = new Student(3.9, 60);

1 Answer

Dimitar Dimitrov
Dimitar Dimitrov
11,787 Points

Because it needs to be this.level === "Senior" you call the get method on the object