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 trialHieu HO
3,975 PointsYour 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.
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);
2 Answers
Steven Parker
231,275 PointsThat's a really unusual but clever use of a "switch" statement!
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.
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
3,975 PointsWow, It look clear, many thanks
Mary Pienzi
Full Stack JavaScript Techdegree Student 7,036 PointsI 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
Hieu HO
3,975 PointsHieu HO
3,975 PointsMy problem was sovled, this is true case this.credits <= 90 && this.credits > 60: return 'Junior'; case this.credits <= 60 && this.credits > 30: