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 trialDan Warren
9,125 PointsHow do I complete this getter method?
How do I finish this code challenge for creating a getter method? I think my if statements are fine but I'm kind of lost when trying to create const objects that allow for my getter method to use credits to determine levels.
class Student {
constructor(gpa, credits){
this.gpa = gpa;
this.credits = credits;
}
get level() {
const level = credits.getLevel();
const credits = student.this.credits;
if(student.this.credits > 90) {
return 'Senior';
} else if(student.this.credits <= 90 && student.this.credits >= 61) {
return 'Junior';
} else if(student.this.credits <= 60 && student.this.credits >= 31) {
return 'Sophomore';
} else if(student.this.credits <= 30) {
return 'Freshman';
}
}
stringGPA() {
return this.gpa.toString();
}
}
const student = new Student(3.9);
1 Answer
John Johnson
11,790 Pointsget level() {
const level = credits.getLevel(); // this will not work, since getLevel() is not yet defined.
const credits = student.this.credits; // you do not need 'students' here: this.credits will work just fine
/* Again, 'students' is not needed at the start of each of these references.
Additionally, if you define credits above as 'this.credits' then you can
just use the credits variable -> if ( credits > 90 ) { return 'Senior' }
*/
if(student.this.credits > 90) {
return 'Senior';
} else if(student.this.credits <= 90 && student.this.credits >= 61) {
return 'Junior';
} else if(student.this.credits <= 60 && student.this.credits >= 31) {
return 'Sophomore';
} else if(student.this.credits <= 30) {
return 'Freshman';
}
This was my answer. I did as much as I could to simplify the code, but you could still use the const credits = this.credits variable in place of using this.credits for each if.
get level() {
if ( this.credits > 90 ) {
return 'Senior';
} else if ( this.credits > 60 ) {
return 'Junior';
} else if ( this.credits > 30 ) {
return 'Sophomore';
} else {
return 'Freshman';
}
}