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 trial

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

God bless you and your family.

8 Answers

Steven Parker
Steven Parker
231,275 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,201 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"; } }

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
231,275 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.

God bless you and your family.

Steven Parker
Steven Parker
231,275 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.

Ayman Omer
Ayman Omer
9,472 Points
class Student {
    constructor(gpa, credits){
        this.gpa = gpa;
        this.credits = credits;
    }


  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";
      } 
  }







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

const student = new Student(3.9);


          ```

Thank you and God bless you and your family.

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
231,275 Points

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

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
231,275 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.

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

get level(){ if (this.credits > 90) { return 'Senior' } else if (this.credits <= 90 && this.credits >= 61) { return 'Junior' } else if (this.credits <= 61 && this.credits >= 31) { return 'Sophomore' } else { return 'Freshman' }

}

Just wondering why you're repeating this.credits instead of putting it in a variable and repeating the variable name?

Steven Parker
Steven Parker
231,275 Points

But this.credits is an attribute variable in the class instance.