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

Solution. Want feedback

This is my solution. Is there more of a streamlined approach someone might be willing to offer?

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 <= 90 && this.credits >= 61) {
      return 'Junior'
    } else if (this.credits <= 60 && this.credits >= 31) {
      return 'Sophomore'
    } else {
      return 'Freshman'
    }
  }

}

const student = new Student(3.9);

console.log(student.level);

It looks good to me. You could simplify it a little bit though. Since else if statements are only executed if the previous condition hasn't been met you could go with something like that:

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

2 Answers

Steven Parker
Steven Parker
231,153 Points

Some very minor compacting tricks:

  • you only need to test conditions not eliminated by previous tests
  • braces can be omitted from single statements
  • a final "else" is not needed if all other conditions end with "return"
  get level() {
    if (this.credits > 90) return 'Senior';
    else if (this.credits > 60) return 'Junior';
    else if (this.credits > 30) return 'Sophomore';
    return 'Freshman';
  }

Thank you both. Very helpful!