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,617 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,354 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
Nnanna Okoli
7,680 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
173,537 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.