Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Object-Oriented JavaScript Getters and Setters Creating Getter Methods

Junior Suarez Peralta
seal-mask
.a{fill-rule:evenodd;}techdegree
Junior Suarez Peralta
Front End Web Development Techdegree Student 11,322 Points

The getter method should return the level of a student, based on how many credits (this.credits) they have.

help, I dont know what I'm doing wrong

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

    stringGPA() {
        return this.gpa.toString();
    }
  get level(){
    let output ="";
    if(this.credits => 90){ output + 'Senior';

    }else if(this.credits <= 61 ){ output +'Junior';

    }
    }else if (this.credits <= 31 ){ output + 'sophomore';

    }else( output += freshman
  )
  return output;


  }
}

const student = new Student(3.9);

2 Answers

A few observations.

  1. You invoke a new Student object, yet you are only passing one of the required values. You need the GPA and the Credits in order to successfully invoke a new Student object.
  2. Your get level method contains a space, which won't work. You'll need either get_level() or getLevel()
  3. After you invoke the new Student object, you need to call the method on the object to return the level. For example:
const student = new Student(3.9, 64);
student.get_level();
  1. Your class method has an arrow function embedded in it which is incorrect. => is attempting to read as an arrow or anonymous function.
  2. You forget the += code in your assignment of the level, instead only using + operator.

Here is the revised code, which when provided with the gpa and credits, correctly returns the level:

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

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

  get_level() {
    let output ="";
    if (this.credits <= 90) {  //removed arrow operator here
        output += 'Senior'; //remember to use += instead of +
    } else if (this.credits <= 61 ) { 
        output += 'Junior';
    } else if (this.credits <= 31 ) { 
        output += 'sophomore';
    } else { 
        output += 'freshman'; 
    }
    return output;}
}

const student = new Student(3.9, 94);
student.get_level();
>>> 'freshman' // this is clearly wrong, so work on the number logic for calculating level.

Happy coding Junior Suarez Peralta! You got this!!!