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 Getters

yoav green
yoav green
8,611 Points

output is "sleeping" even ifs false in the condition. cant understand why

class Pet {
  constructor(animal, age, bread, sound){
    this.animal = animal;
    this.age = age;
    this.bread = bread;
    this.sound = sound;
  }

  get activity() { //getter method//
    const today = new Date();
    const hour = today.getHours();

    if (hour > 8 && hour <=20) {
      return 'playing';
    } else {
      return 'sleeping';
    }

    if (hour > 16 && hour < 23) {
      return 'meeting the Bros in the hood';
    } else {
      return 'at home, board to death';
    }

  }

  speak() {
    console.log(this.sound);
  };

}

const ernie = new Pet('dog', 1, 'pug', 'woof woof');
const vera = new Pet('cat', 5, 'persian', 'meouuu');

console.log(ernie);

4 Answers

Steven Parker
Steven Parker
216,148 Points

The "else" action is always performed if the "if" test is false. And since both the "true" and "false" conditions cause a return, the function will end before going on to the next "if".

What you probably want is a chain of "else if" statements to check all the other possible conditions before the final "else".

yoav green
yoav green
8,611 Points

thanks alot Steven.

Steven Parker
Steven Parker
216,148 Points

Here's an example of what such a chain might look like:

    if (hour > 8 && hour <=20) {
      return 'playing';
    } else if (hour == 21) {
      return 'meeting the Bros in the hood';
    } else if (hour > 21) {
      return 'at home, board to death';
    } else {
      return 'sleeping';
    }
yoav green
yoav green
8,611 Points

another question. even if i erase the second "if" and "else", and the first if statement is still " if (hour > 8 && hour <=20)" i get the 'playing' return. even though it's after 20 in my time. what can be the reason?

Steven Parker
Steven Parker
216,148 Points

That doesn't seem possible. Try this to be sure the hour is what you are expecting:

      return 'playing at ' + hour;

If that doesn't reveal the issue, you can facilitate a thorough analysis if you make a snapshot of your workspace and post the link to it here.

yoav green
yoav green
8,611 Points

and i didn't manage to to preform your first answer. maybe you can help me with the code itself? do i need to create another "getter" or can i apply all the conditional statements in one "getter"? thanks alot!

Steven Parker
Steven Parker
216,148 Points

I assumed it would go in the same function. I added an example to my answer.

yoav green
yoav green
8,611 Points

used that - " return 'playing at ' + hour; " gave me the hour 18, even though the hour here is 21:54.

Steven Parker
Steven Parker
216,148 Points

Perhaps the time zone is set incorrectly? You can use "new Date().getTimezoneOffset()" to see how far off (in minutes) from UTC the system thinks you are.