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 Getter Methods

Andrea Rios
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrea Rios
Front End Web Development Techdegree Student 10,078 Points

The getter method should return the level of a student, based on how many credits (this.credits) they have.

Why is this not working? :(

class Student { constructor(gpa, credits){ this.gpa = gpa; this.credits = credits; }

stringGPA() {
    return this.gpa.toString();
}

get level() { if (credits>90) { return 'Senior'; } if else (credits =< 89 && credits > 60) { return 'Junior'; } if else (credits=<60 && credits >= 31) { //60 or fewer credits, but more than 30 (>= 31), they are a 'Sophomore'. return 'Sophmore'; } else (credits=<30) { return 'Freshman'; } //30 or fewer credits, they are a 'Freshman'. } }

const student = new Student(3.9);

creating_getters.js
class Student {
    constructor(gpa, credits){
        this.gpa = gpa;
        this.credits = credits;
    }

    stringGPA() {
        return this.gpa.toString();
    }

  get level() {
    if (credits>90) {
      return 'Senior';
    } if else (credits =< 89 && credits > 60) {
      return 'Junior';
    } if else (credits=<60 && credits >= 31) {
      //60 or fewer credits, but more than 30 (>= 31), they are a 'Sophomore'.
      return 'Sophmore';
    } else (credits=<30) {
      return 'Freshman';
    }
      //30 or fewer credits, they are a 'Freshman'.
  }
}

const student = new Student(3.9);

1 Answer

Steven Parker
Steven Parker
231,271 Points

There are a few issues:

  • the instance variable "credits" needs to have the "self." prefix
  • the operator for "less than or equal" is "<=" (instead of "=>")
  • the keywords for "else if" are written out of order
  • a plain "else" does not take a conditional expression
  • the correct spelling of "Sophomore" should have 3 o's (instead of "Sophmore")
  • the credit value of exactly 90 is not being correctly identified by the conditional expression