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

gael blanchemain
seal-mask
.a{fill-rule:evenodd;}techdegree
gael blanchemain
Full Stack JavaScript Techdegree Student 8,843 Points

"your conditional statement is returning the wrong student level"

I am trying to pass the "getter" test: https://teamtreehouse.com/library/objectoriented-javascript-2/getters-and-setters/creating-getter-methods

My code passes with node js, but not in the treehouse compiler, any hint anyone? Thank you! Here is my code

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

    stringGPA() {
        return this.gpa.toString();
    }
  get level() {
      let gpa = this.stringGPA()
    if (gpa > 90) {
     return 'Senior' 
    }else if (gpa >= 61){
        return 'Junior'
    }else if (gpa >= 31){
        return 'Sophomore'
    } else if (gpa <= 30){
        return 'Freshman'
  }
}
};

const student = new Student(3.9);

console.log(student.level)

2 Answers

Brandon White
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 Points

Hi gael,

I think the issue you’re having is that you’re comparing the wrong data. The challenge asks that you determine the levels based off of this.credits (not this.stringGPA).

Assuming that the interpreter will compare strings and numbers (because I believe the challenges are already in strict mode), any GPA will always be so small as a number that it will always return Freshman.

Now, it IS odd that in the student variable that they create below the class definition that they haven’t passed in an argument for credits. And maybe that threw you off and is the reason you used the stringGPA as the data you’re testing, but ignore what’s been written on the line below the class definition. Just focus on the instructions and write your code based off that.

gael blanchemain
seal-mask
.a{fill-rule:evenodd;}techdegree
gael blanchemain
Full Stack JavaScript Techdegree Student 8,843 Points

I applied your recommendations, but it seems not to run this time again, if you could enlighten me, I would be really grateful!

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

    stringGPA() {
        return this.gpa.toString();
    }
  get level() {
      let gpa = this.gpa;
    if (gpa > 90) {
     return 'Senior' 
    }else if (gpa >= 61){
        return 'Junior'
    }else if (gpa >= 31){
        return 'Sophomore'
    } else if (gpa <= 30){
        return 'Freshman'
  }
}
};

const student = new Student(3.9);

console.log(student.level)
Brandon White
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brandon White
Full Stack JavaScript Techdegree Graduate 35,771 Points

It's possible that I've confused you, gael because you're still not checking this.credits when trying to return the level.

app.js
class Student {
    constructor(gpa, credits) {
        this.gpa = gpa;
        this.credits = credits; // This is the value you should be testing
    }

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

    get level() {
      // let gpa = this.gpa; // this line you can remove

      if (this.credits > 90) { // instead of checking gpa, you want to check this.credits
        return 'Senior' 
      } else if (this.credits >= 61) { // same thing here
        return 'Junior'
      } else if (this.credits >= 31) { // and here
        return 'Sophomore'
      } else if (this.credits <= 30) { // and here
        return 'Freshman'
      }
    }
};

const student = new Student(3.9); // it's a bit weird that an argument isn't passed in to satisfy the credits parameter, and as someone who's still learning the ropes it may lead you to assume that you should be focused on the gpa variable in the constructor function, but for this challenge you shouldn't be 

console.log(student.level) // You don't need this line, but I don't think it would mess up the challenge. In the future though, logging things to the console can result in you not passing a challenge, so it's best not to add code that the challenge doesn't request

Look over the code I've added, and when you feel like you understand where you were a little off, complete this challenge and keep moving forward.