JavaScript Object-Oriented JavaScript Getters and Setters Creating Setter Methods

Kristoffer A-L
Kristoffer A-L
5,557 Points

Struggling with get and set in JavaScript

Hi guys, I can't seem to get this right, would really appreciate the help.

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

}

var student = new Student(3.9, 60);

1 Answer

rydavim
MOD
rydavim
Treehouse Moderator 16,153 Points

Looks like just a couple of minor issues may be blocking your challenge completion, you've got the general principles down.

// This shouldn't block your challenge completion, but I don't think you need it.
get major() {
    return this._major
  }

  set major(major) {
    // Shouldn't need this bit either, you're setting it below.
    this._major = major;

    // I think you need this.level in place of student.level here.
    // You also probably need to repeat your comparison, I don't think your shorthand works.
    // this.level === <condition> || this.level === <condition>    
    if (student.level === 'Junior' || 'Senior') {
      this._major = major;
    } else if (student.level === 'Freshman' || 'Sophomore') {
      this._major = 'None';
    }
  }

That should get you on your way, but let me know if you still run into trouble and we'll walk through a solution. Nice job, and happy coding!

Kristoffer A-L
Kristoffer A-L
5,557 Points

Thank you so much rydavim, changing student.level to this.level did it!

Best regards, Kris