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 trialgladys padilla
7,909 PointsThe getter method should return the level of a student, based on how many credits (this.credits) they have.
I am completely stuck. I don't see how else I could solve this problem. Someone help?
class Student {
constructor(gpa, credits){
this.gpa = gpa;
this.credits = credits;
}
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 if (this.credits <= 30) {
return 'Freshman';
}
}
stringGPA() {
return this.gpa.toString();
}
}
const student = new Student(3.9);
8 Answers
Steven Parker
231,269 PointsYou're really close! Just check your comparison symbols, in particular:
-
if (this.credits >= 90) {
this tests for greater or equal to 90 -
} else if (this.credits <= 60 && this.credits <= 31) {
only true when less than or equal to 31
Alexandre Formoso
10,201 Pointsget level() { if (this.credits > 90) { return "Senior"; } else if (this.credits >= 61) { return "Junior"; } else if (this.credits >= 31) { return "Sophomore"; } else { return "Freshman"; } }
Conor Vanoystaeyen
16,687 PointsHey gladys padilla,
The way you solve the exercise is not a bad idea, but if you want to make the code shorter. make a variable that prints the output and then prints it out at the end.
example:
get level () {
let output = "";
if (this.credits > 90) output += "Senior";
if (this.credits <= 90 && this.credits >= 61 ) output += "Junior";
if (this.credits <= 60 && this.credits >= 31 ) output += "Sophomore";
if (this.credits <= 30) output += "Freshman";
return output;
}
Runs through all ifs until it finds the right one and stores the string in output. And no more unnecessary returns and the structure is easier to read.
Steven Parker
231,269 PointsBut the "return" statements can make the code even more compact, and no variable needed:
get level() {
if (this.credits > 60) return "Top";
if (this.credits > 40) return "Upper";
if (this.credits > 20) return "Middle";
return "Lower";
}
Note that this example has been altered so it is not valid for solving the challenge.
Mauricio Hernandez
7,208 PointsGod bless you and your family.
teresemartyn
Front End Web Development Techdegree Graduate 21,880 PointsConor & Stephen - thanks. Appreciate you sharing that code. That worked well. I implemented it with a different scenario. Terese
Steven Parker
231,269 PointsGlad to hear you didn't just copy a solution, good job!
And that made me realize that these examples don't actually need to be functional to make their point, so I altered them so they cannot be pasted in as solutions.
Ayman Omer
9,472 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);
```
Mauricio Hernandez
7,208 PointsThank you and God bless you and your family.
Conor Vanoystaeyen
16,687 PointsHey Steven Parker,
Ooooh nice that I had not thought of always nice to see how other people solve this problem. thx Steven is there a shorter way or is 4 lines the least?
Steven Parker
231,269 PointsAre you familiar with the "ternary" expression? You can do it in one line that way.
Conor Vanoystaeyen
16,687 PointsYes if I am not mistaken it is this (?) but that makes the choices between 2 variablens (true or false) how can you write it in one line then?
Steven Parker
231,269 PointsThey can be nested:
get level() {
return this.credits > 33 ? "Ace" :
this.credits > 22 ? "King" :
this.credits > 11 ? "Queen" : "Jack";
}
For visual clarity I split it across 3 lines but it's just one statement.
Note that this is for example only and I'm not advocating it as a better solution.
Also note that this example has been altered so it is not valid for solving the challenge.
Conor Vanoystaeyen
16,687 PointsWow really nice man thank you. Have learned a lot.
Mat T'Qartit
29,277 Pointsget level(){ if (this.credits > 90) { return 'Senior' } else if (this.credits <= 90 && this.credits >= 61) { return 'Junior' } else if (this.credits <= 61 && this.credits >= 31) { return 'Sophomore' } else { return 'Freshman' }
}
Olivia H
3,842 PointsJust wondering why you're repeating this.credits instead of putting it in a variable and repeating the variable name?
Steven Parker
231,269 PointsBut this.credits is an attribute variable in the class instance.
Mauricio Hernandez
7,208 PointsMauricio Hernandez
7,208 PointsGod bless you and your family.