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

Reagan Ganancial
Reagan Ganancial
9,476 Points

This is the answer for this task..

This is it..

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

    stringGPA() {
        return this.gpa.toString();
    }
  get level() {
     const credits = this.credits;
    if (credits > 90) {
      return 'senior';
    } else if (credits > 61 && credits <= 90) {
      return 'junior';
    } else if (credits > 30 && credits <= 60) {
      return 'sophomore';
    } else {
      return 'freshman';
    }    
  }
}

const student = new Student(3.9);
Steven Parker
Steven Parker
229,644 Points

:x: This does not pass the challenge (did you try it yourself?); and even if it was fixed, it would just be one possible answer for this task, and certainly not the most efficient. And why would you want to rob another student of the experience of creating the answer themselves?

:warning: You should also be aware that explicit answers without any explanation (or in this case, not even any question) are strongly discouraged by Treehouse and may be redacted by moderators.

2 Answers

Steven Parker
Steven Parker
229,644 Points

I think your issue may be that this challenge involves the use of the "this" keyword but that it hasn't been introduced yet. If so, that's a valid point and I ecourage you to report it directly to the Support staff.

But this code has other issues:

  • the challenges are very picky about output strings, and these are all lower case (unlike the instructions)
  • there's a gap in the number ranges, and the value returned when "credits" is 61 will be incorrect.
Reagan Ganancial
Reagan Ganancial
9,476 Points

the challenges are very picky about output strings... this one is my biggest problem on it..

I want to challenge you Steven Parker! This course is the new version of object oriented javascript (2018) it talks about class type programming. The old one; object oriented javascript (2015) talks about prototype and inheritance. In it's latest challenge was the Quiz challenge.. I don't know how to adapt the class type programming in Quiz challenge way back 2015. I think if only Teamtreehouse do some workshop on it how to make a project out of class type object oriented programming.. You could use this link.. Use the lesson from the object oriented javascript (2018) and don't use the prototype inheritance.

https://teamtreehouse.com/library/quiz-prototype

I'll be doing it and ill paste it with a KEYWORD.. object oriented javascript (2018) Quiz Challenge.. Then let see how you would tackle it in a effective lesser code.. You love some challenges mate! And here it is!

Steven Parker
Steven Parker
229,644 Points

There's actually a pretty direct correspondence between the classic syntax and the new syntax. Here's a "new syntax" version of the file shown in that video:

quiz.js
// revised for ES6 syntax 04/30/18.sp
class Quiz {
    constructor(questions) {
        this.score = 0;
        this.questions = questions;
        this.currentQuestionIndex = 0;
    }

    guess(answer) {
        if(this.getCurrentQuestion().isCorrectAnswer(answer)) {
            this.score++;
        }
        this.currentQuestionIndex++;
    }

    getCurrentQuestion() {
        return this.questions[this.currentQuestionIndex];
    }

    hasEnded() {
        return this.currentQuestionIndex >= this.questions.length;
    }
}
Reagan Ganancial
Reagan Ganancial
9,476 Points

const credits = this.credits;

I post this answer so that treehouse will give us a concise instructions in quizzes. I have done so many trial and errors because the result ask us the given const. Well.. try it and for treehouse.. Give us a specific instruction.. PLEASE!