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

JS Object Oriented-Class and Getter

I kept getting an error saying my getter isn't returning a value. Please help. Thank you

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

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

    get level(){
      const currentCredits = this.credits
    if (currentCredits > 90) {
      return 'Senior';
    } else if (currentCredits <= 90 && currentCredits > 61) {
      return 'Junior';
    } else if(currentCredits <= 60 && currentCredits > 31) {
       return 'Sophomore';  
    } else if(currentCredits <= 30) {
      return 'Freshman';
    }
}
}

const student = new Student(3.9);
console.log(Student.level);

2 Answers

Nvm...I got the answer to my question :)

Here's the solution in case anyone is looking:

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';
    }
}

} const student = new Student(3.9); console.log(Student.level);

Cameron Childres
Cameron Childres
11,817 Points

Hi Thanh,

Your first answer was very close to working. The issue is when students have credits of 61 or 31 -- there's no logic that covers those values since you used greater than. If you change it to "currentCredits >= 61" and "currentCredits >= 61" your code works fine.