JavaScript Object-Oriented JavaScript Getters and Setters Creating Getter Methods

Martin Coutts
Martin Coutts
18,154 Points

Unexpected Token

Not sure what my issue here is, think that my conditional statements are ok but it keeps saying there is an unexpected token with <= when I remove the equals it is an unexpected token with < and if I change that there is something else unexpected

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

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

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

const student = new Student(3.9);

1 Answer

Adam Beer
Adam Beer
11,269 Points

Hi Martin! You are near! I just little changed your code. Please check, I use "this" keyword before credits in if/else statement. Next I delete your two statements inside else if. Finally I correcting your number inside the last else if statement. I transcribed 31 to 30. Like this.

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

get level(){
        if(this.credits > 90){
            return 'Senior';
        } else if(this.credits > 60){
            return 'Junior';          
        } else if(this.credits > 30){
            return 'Sophomore';          
        } else {
            return 'Freshman';
        }
    }
Martin Coutts
Martin Coutts
18,154 Points

Thanks Adam. Still a bit unsure when to use this and when not to. I'm guessing the code would have ran with the larger conditional statements it just wasn't what the challenge was looking for? I can see looking at your example it is much cleaner and simpler.

Adam Beer
Adam Beer
11,269 Points

I sent for you a link how can you use "this" keyword, please check. Then you will understand. The function did not know where to look for the credits but now it find inside the constructor. With the logical operators had a small problem so the error message was "unexpected token"

Vladimir Plokhotniuk
Vladimir Plokhotniuk
5,464 Points

When i have "=> 60 && <= 90" i receiving "Unexpected Token => ". Why? ...and in your example, number more then 90 should be suitable for all options?