Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 9,401 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
217,443 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