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

Nima Sakha
seal-mask
.a{fill-rule:evenodd;}techdegree
Nima Sakha
Front End Web Development Techdegree Student 10,645 Points

how to do this question

i cant seem to figure this out

creating_getters.js
class Student {
    constructor(gpa, credits){
        this.gpa = gpa;
        this.credits = credits;
    }
    get level(){
      const credits = this.credits;
      if(credits > 90){
        return 'Senior';
      } 
      else if (credits < 90 && credits >= 61){
        return 'Junior';
      }
      else if (credits < 60 && credits >= 31){
        return 'Sophomore';
      }
      else if (credits < 30){
        return 'Freshman';
      }
    }

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

const student = new Student(3.9, 50);

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,799 Points

Hi Nima. You are super close. Let me ask you. What happens if a student has 90, 60 or 30 credits? You are checking for > 90 and < 90 but never 90. A quick and dirty fix to pass the challenge is to change the conditions to credits <= like this:

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

BUT

Do we even need those conditions?

If a student has more than 90 credits they are a senior. So do we need to check if a student has <= 90 credits when we are checking for a junior? No. Because if they had more that 90 they would be a senior. So when we get to the first else we know the student has less or equal 90 credits. We only need to check if credits >= 61 because that makes them a junior.

Same for Sophomores. We know they have <= 60 credits or they would be a junior or senior.

Then for Freshman you don't need any condition. That can just be an else because if they are not a senior, junior or sophomore then they have to be a freshman

Hope this helps and be sure to post back to the community if you are still stuck.