JavaScript Object-Oriented JavaScript Getters and Setters Creating Getter Methods

Andrew Federico
Andrew Federico
7,483 Points

Getter method is not returning a value.

I submitted it the first time and it worked, but then I refreshed the page to go back and play to see if it would accept waterfall conditionals and single lines without (){return}, and now it won't accept it at all, even the other way.

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

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

    get level() {
        const credit = this.credits;

        if (credit > 90) {
            return 'Senior';
        } else if (credit < 90 && credit >= 61) {
            return 'Junior';
        } else if (credit < 60 && credit >= 31) {
            return 'Sophomore';
        } else if (credit < 30) {
            return 'Freshmen';
        }

    }
}

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

3 Answers

Eric McKibbin
MOD
Eric McKibbin
Treehouse Moderator 11,441 Points

Hi Andrew,

What happens in your code if the student has exactly 90 credits? Or 60, or 30?

While this might work for the majority of cases it's going to have some bugs in production!

Your code otherwise looks good so I'm sure you can figure out how to resolve this minor issue :) You'll also need to change "Freshmen" to "Freshman" to pass the coding challenge.

Cheers,

Eric

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there, Andrew Federico ! I'm not exactly sure what your code for your "other" way looked like, but there are several problems in this version of the code. First, you say what happens if credit is greater than 90 and what happens if it is less. But you fail to say what happens if it is exactly equal to 90. So in your first else if you should be checking if it is less than or equal to 90. This sort of pattern continues downwards. In the next part, you say what happens if it is greater than or equal to 61 or less than 60, but you fail to say what happens if it is exactly equal to 60. You should be checking to see if credit is less than or equal to 60. The same thing goes for the last check. You say what happens when it's greater than or equal to 31 and what happens when it's less than 30, but there is no rule for when it is exactly 30. Furthermore, here you've typed the plural "Freshmen" instead of the singular "Freshman" which will result in the incorrect string being returned.

Hope this helps! :sparkles:

Andrew Federico
Andrew Federico
7,483 Points

Thank you both, yeah that was it. Just needed <= for 90, 60, 30. Oddly, it took it the first time without it, so glad it didn't take it the second so I could be corrected on my oversight. Thanks.