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 Setter Methods

Why can't I access the level() getter methods' output?

Previously the level had been set already using the getter method and the property 'credits' from the constructor method.

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

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

    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';
        }
    }
    // why won't this work?
    set major() {
        if (this.level === 'Senior' || this.level === 'Junior' ) {
            _major = major;
        } else if (this.level === 'Sophomore' || this.level === 'Freshman' ) {
            _major = 'None';
        }
    }
}

I can't invoke a getter method with this?

1 Answer

Rachel KTY Johnson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rachel KTY Johnson
Treehouse Project Reviewer

Hey Samuel Kleos ! I'm not 100% sure how to interpret your question, as this.level definitely returns something. But I can share some things that are missing from your code.

  1. The get major() setter method needs a "formal parameter", meaning you'll need to pass in a major parameter, like so: get major(major). Afterall, if we're setting something, we'll be passing in some data.

  2. The backing property _major needs to be accompanied by a this, as it belongs to this instance, for example: this._major = major

I hope this helps!