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 trialBruno Antonellini
7,168 PointsSwitch is behaving weirdly
As far as i'm concerned, the Switch Case of my code should be working properly, but it isn't. Any clue?
Thanks in advance!
class Student {
constructor(gpa, credits){
this.gpa = gpa;
this.credits = credits;
}
get level() {
switch (this.credits) {
case this.credits > 90:
return 'Senior';
break;
case this.credits > 61 && this.credits <= 90:
return 'Junior';
break;
case this.credits > 31 && this.credits <= 60:
return 'Sophomore';
break;
case this.credits <= 30:
return 'Freshman';
break;
default:
return 'Invalid credits';
break;
}
}
stringGPA() {
return this.gpa.toString();
}
}
const student = new Student(3.9, 60);
Bruno Antonellini
7,168 PointsDave varmutant quote: "Bummer: Your conditional statement is returning the wrong student level."
Antti Lylander
9,686 PointsJust a sidenote: you don't need breaks as return
breaks it already.
5 Answers
Adam Beer
11,314 PointsUse conditional statement, so please rewrite your code with if/else statement. Hope this help.
Challenge Task 2 of 2
Inside the getter method use the conditional statement of your choice to determine the level of the student. If the value of the "credits" property is over 90, return 'Senior'. If it's between 61 and 90, return 'Junior'. If it's between 31 and 60, return 'Sophomore'. If it's 30 or less, return 'Freshman'.'
Adam Beer
11,314 PointsInside the 2 else if statements please delete the second statements. And change your value to this.credits > 60 and this.credits > 30. If the statement is true, then don't give the property last else to conditional.
Bruno Antonellini
7,168 PointsAdam Beer Switch Case IS, in fact, a conditional. I've already tried with the tedious If ... Else If ... Else structure, but it didn't work as well.
I'll give it another try and post my code and results.
Thanks!
Adam Beer
11,314 PointsPlease try again! If it does not go then I'll show you the code.
Bruno Antonellini
7,168 PointsDone! My fault setting >61 and >31 instead of 60 and 30.
You made me read the instructions again and find out i'm an idiot.
Have a great weekend dude!
Sean T. Unwin
28,690 PointsFYI switch
is valid here and, in my opinion, is so much clearer and cleaner.
Be sure to keep an eye on the criteria and spelling of the levels.
get level() {
let level = '';
switch (true) {
case (this.credits > 90) :
level = 'Senior';
break;
case (this.credits <= 90 && this.credits > 60) :
level = 'Junior';
break;
case (this.credits <= 60 && this.credits > 30) :
level = 'Sophomore';
break;
default :
level = 'Freshman';
break;
}
return level;
}
Bruno Antonellini
7,168 Pointsclass Student { constructor(gpa, credits){ this.gpa = gpa; this.credits = credits; }
get level() {
if (this.credits > 90) {
return 'Senior';
} else if (this.credits > 61) {
return 'Junior';
} else if (this.credits > 31) {
return 'Sophomore';
} else {
return 'Freshman';
}
}
stringGPA() {
return this.gpa.toString();
}
}
const student = new Student(3.9, 25);
Bruno Antonellini
7,168 PointsDon't know why part of the code is inside the grey box and the rest is outside, but it's still pretty clear.
I'm starting to consider that the Check Work algorithm is broken haha
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 PointsWhat isn't working properly?