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 trialmichael conour
5,011 PointsSolution. 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);
2 Answers
Steven Parker
231,153 PointsSome 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';
}
michael conour
5,011 PointsThank you both. Very helpful!
tapio
Full Stack JavaScript Techdegree Graduate 25,832 Pointstapio
Full Stack JavaScript Techdegree Graduate 25,832 PointsIt 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: