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 trialRemi Vledder
14,144 PointsWhy use the comparison instead of it increasing (>=31 as opposed to >= 30)
In the description there is a hint where it states to use >=31 for comparing the credits of the student. That way you can check if the credits are lower than 31 to return for a Freshman.
What would be the reasoning to do so?
Since doing it the other way around also seems to be working:
class Student {
constructor(gpa, credits){
this.gpa = gpa;
this.credits = credits;
}
get level() {
// less than or equal to 30
if (this.credits <= 30) {
return 'Freshman';
// between 30 and 60
} else if (this.credits >= 30 && this.credits <= 60) {
return 'Sophomore';
// between 60 and 90
} else if (this.credits >= 60 && this.credits <= 90) {
return 'Junior';
// everything greater than 90
} else if (this.credits >= 90) {
return 'Senior';
}
}
stringGPA() {
return this.gpa.toString();
}
}
// const student = new Student(3.9, 10); // output Student
// const student = new Student(3.9, 31); // output Sophomore
// const student = new Student(3.9, 61); // output Junior
// const student = new Student(3.9, 91); // output Senior
I'm interested in the cons or pro's of these two different approaches.
1 Answer
Steven Parker
229,644 PointsYour first "if" takes care of everything up to and including 30. So it doesn't matter if the "else if" range includes 30 or starts above it, since it will never be used to test a value of 30 or less.
But just for clarity for someone reading the code, it would be better for the ranges to not overlap. But even better, you can leave off the redudant lower testing completely to make the code more compact, particularly when the comment makes the line's function clear:
// between 31 and 60
} else if (this.credits <= 60) {
Remi Vledder
14,144 PointsRemi Vledder
14,144 PointsThanks for the reply. Not sure if I get it. Is there a more concise way of writing this or is it already optimal in it's current form?
Steven Parker
229,644 PointsSteven Parker
229,644 PointsLeaving off the redundant lower test makes it more concise. I added an example to my answer.
Also, the final "else" doesn't need an "if" (or a condition) at all.
Remi Vledder
14,144 PointsRemi Vledder
14,144 PointsMethod 1: 237 characters (not including comments):
Method 2: 261 characters (not including comments):
Steven Parker
229,644 PointsSteven Parker
229,644 PointsConcise, readable, and no redundant testing either way.
Though on the 2nd one, you might want to indicate the complete ranges in the comments, for example:
// 61 through 90 (instead of "61 and higher")