JavaScript Object-Oriented JavaScript Getters and Setters Creating Getter Methods

gladys padilla
gladys padilla
7,909 Points

The 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?

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 <= 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);

5 Answers

Steven Parker
Steven Parker
172,034 Points

You're really close! Just check your comparison symbols, in particular:

  • if (this.credits >= 90) { :point_left: this tests for greater or equal to 90
  • } else if (this.credits <= 60 && this.credits <= 31) { :point_left: only true when less than or equal to 31
Alexandre Formoso
Alexandre Formoso
10,098 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"; } }

Conor Vanoystaeyen
Conor Vanoystaeyen
16,649 Points

Hey 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
Steven Parker
172,034 Points

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

Conor & Stephen - thanks. Appreciate you sharing that code. That worked well. I implemented it with a different scenario. Terese

Steven Parker
Steven Parker
172,034 Points

Glad to hear you didn't just copy a solution, good job! :+1:

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.

Conor Vanoystaeyen
Conor Vanoystaeyen
16,649 Points

Hey 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
Steven Parker
172,034 Points

Are you familiar with the "ternary" expression? You can do it in one line that way.

Conor Vanoystaeyen
Conor Vanoystaeyen
16,649 Points

Yes 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
Steven Parker
172,034 Points

They 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
Conor Vanoystaeyen
16,649 Points

Wow really nice man thank you. Have learned a lot.