JavaScript Object-Oriented JavaScript Getters and Setters Creating Getter Methods

Bruno Antonellini
Bruno Antonellini
7,168 Points

Switch is behaving weirdly

As far as i'm concerned, the Switch Case of my code should be working properly, but it isn't. Any clue?

Thanks in advance!

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

    get level() {      
      switch (this.credits) {
        case this.credits > 90:
            return 'Senior';
            break;
        case this.credits > 61 && this.credits <= 90:
            return 'Junior';
            break;
        case this.credits > 31 && this.credits <= 60:
            return 'Sophomore';
            break;
        case this.credits <= 30:
            return 'Freshman';
            break;
        default:
            return 'Invalid credits';
            break;
      }
    }

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

const student = new Student(3.9, 60);
Bruno Antonellini
Bruno Antonellini
7,168 Points

Dave varmutant quote: "Bummer: Your conditional statement is returning the wrong student level."

Antti Lylander
Antti Lylander
9,660 Points

Just a sidenote: you don't need breaks as return breaks it already.

5 Answers

Adam Beer
Adam Beer
11,269 Points

Use conditional statement, so please rewrite your code with if/else statement. Hope this help.

Challenge Task 2 of 2

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

Adam Beer
Adam Beer
11,269 Points

Inside the 2 else if statements please delete the second statements. And change your value to this.credits > 60 and this.credits > 30. If the statement is true, then don't give the property last else to conditional.

Bruno Antonellini
Bruno Antonellini
7,168 Points

Adam Beer Switch Case IS, in fact, a conditional. I've already tried with the tedious If ... Else If ... Else structure, but it didn't work as well.

I'll give it another try and post my code and results.

Thanks!

Adam Beer
Adam Beer
11,269 Points

Please try again! If it does not go then I'll show you the code.

Bruno Antonellini
Bruno Antonellini
7,168 Points

Done! My fault setting >61 and >31 instead of 60 and 30.

You made me read the instructions again and find out i'm an idiot.

Have a great weekend dude!

Sean T. Unwin
MOD
Sean T. Unwin
Treehouse Moderator 28,374 Points

FYI switch is valid here and, in my opinion, is so much clearer and cleaner.

Be sure to keep an eye on the criteria and spelling of the levels.

    get level() {
      let level = '';

      switch (true) {
        case (this.credits > 90) :
          level = 'Senior';
          break;
        case (this.credits <= 90 && this.credits > 60) :
          level = 'Junior';
          break;
        case (this.credits <= 60 && this.credits > 30) :
          level = 'Sophomore';
          break;
        default :
          level = 'Freshman';
          break;
      }

      return level;
    }
Bruno Antonellini
Bruno Antonellini
7,168 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, 25);

Bruno Antonellini
Bruno Antonellini
7,168 Points

Don't know why part of the code is inside the grey box and the rest is outside, but it's still pretty clear.

I'm starting to consider that the Check Work algorithm is broken haha