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

Kristoffer A-L
Kristoffer A-L
5,863 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
rydavim
18,814 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,863 Points

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

Best regards, Kris