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

Hieu HO
Hieu HO
3,975 Points

Your conditional statement is returning the wrong student level.

I used switch case in my case, but it's not working while i tested in my workspaces is truth.

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

    stringGPA() {
        return this.gpa.toString();
    }
  get level(){
    switch(true){
      case this.credits>90:
        return 'Senior';
      case this.credits < 90 && this.credits > 60:
        return 'Junior';
      case this.credits < 60 && this.credits > 30:
        return 'Sophomore';
      default:  return 'Freshman';
    }
  }
}

const student = new Student(3.9);
Hieu HO
Hieu HO
3,975 Points

My problem was sovled, this is true case this.credits <= 90 && this.credits > 60: return 'Junior'; case this.credits <= 60 && this.credits > 30:

2 Answers

Steven Parker
Steven Parker
231,269 Points

That's a really unusual but clever use of a "switch" statement! :+1:

But you're not covering all possible values in your ranges. You can fix it by changing the comparison operators, but you can also simplify the tests by only checking the upper range in each case:

    switch (true) {
      case this.credits > 90:
        return 'Senior';
      case this.credits > 60:
        return 'Junior';
      case this.credits > 30:
        return 'Sophomore';
      default:
        return 'Freshman';
    }

This same logical test reduction would work in an "else if" chain, which would be more conventional and perhaps easier to read. :wink:

      if (this.credits > 90)      return 'Senior';
      else if (this.credits > 60) return 'Junior';
      else if (this.credits > 30) return 'Sophomore';
      /* else */                  return 'Freshman';
Hieu HO
Hieu HO
3,975 Points

Wow, It look clear, many thanks

Mary Pienzi
seal-mask
.a{fill-rule:evenodd;}techdegree
Mary Pienzi
Full Stack JavaScript Techdegree Student 7,036 Points

I feel like my eyes have been washed. Even though I have seen statements look like this I now understand and can actually do it myself. Thanks