Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Getters and Setters Creating Getter Methods

Inside the getter method use the conditional statement of your choice to determine the level of the student. If the valu

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'.'

creating_getters.js
class Student {
    constructor(gpa, credits){
        this.gpa = gpa;
        this.credits = credits;
    }

   get level( ) {
      if (this.credits >= 90 ) {   
      return 'Senior'

      }else if ( this.credits >=61 && this.credits<=90){
       return 'Junior'}

      else if ( this.credits >= 31 && this.credits<= 60){
      return 'Sophomore'}

      else ( this.credits <= 30){
      return 'Freshman' }  

      }

    stringGPA() {
        return this.gpa.toString();
    }
}

const student = new Student(3.9);

5 Answers

Julian Cobos
Julian Cobos
10,645 Points

I find this website to be very useful for JS. https://www.w3schools.com/jsref/default.asp

class Student {
    constructor(gpa, credits){
        this.gpa = gpa;
        this.credits = credits;
    }
    stringGPA() {
        return this.gpa.toString();
    }
    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";
        }
    }
}

const student = new Student(3.9);
Martin Lecke
Martin Lecke
14,371 Points
...
  if (this.credits >= 90 ) {   
     return 'Senior'
  }  
...

When you write >= You are saying this.credits should be above 90 but it can also be 90. Which is not over 90 which we are looking for in this case.


Also you are not needed to write;
this.credits >=61 && this.credits<=90 There is no need to state a range between numbers since your if-statement will read top to bottom if things are true. Since the higher if statements doesnt turn true it means its not above 90, and its not above 60

  get level() {

    if(this.credits > 90) { // is my credit above 90? - no
     return 'Senior'; 
    } else if(this.credits > 60) { // is my credit above 60 then? - no
     return 'Junior'; 
    } else if (this.credits > 30) { // But my credit is above 30? - yes
     return 'Sophomore';  // this code runs
    } else { // everything less than 30 would have run here, even negative credits
     return 'Freshman'; 
    } 

  }
Nnanna Okoli
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nnanna Okoli
Front End Web Development Techdegree Graduate 19,167 Points

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' } }
}

IS YOUR CORRECT ANSWER

Steven Parker
Steven Parker
218,691 Points

You're really close, but I see two issues:

A plain "else" doesn't take (or need) a conditional expression.

Also, the instructions say "If the value of the "credits" property is over 90, return 'Senior'", but this code will also return 'Senior' when the credits are exactly 90.

So just remove the unneeded conditional and change your comparison from ">=" to just ">" and you'll have it.