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

Junior Suarez Peralta
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Junior Suarez Peralta
Front End Web Development Techdegree Graduate 15,160 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

Fergus Clare
Fergus Clare
12,120 Points

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.
Fergus Clare
Fergus Clare
12,120 Points

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