Creating Classes and Calling a Class Method

Creating Classes and Calling a Class Method
0

#1

/*The class Puppy needs two methods.

The getsTreat method should increase happiness by 20, then return the new happiness value.

The takesNap method should decrease energy by 45 and increase behavior by 15.

Make sure you make the puppy take his nap in order to change his energy and behavior levels*/

/* When I console.log(Charles) after the construction of the Puppy Class it returns :

“Puppy { happiness: 75, energy: 25, behavior: 30 }”

which is the value at the initial construction. Am I improperly calling the “getTreat” and “takesNap” methods to change the puppies value?? Do I need to add a return statement in these methods??*/

class Puppy {

  constructor(happiness, energy, behavior) {
    this.happiness = happiness;
    this.energy = energy;
    this.behavior = behavior;
  }

  getsTreat() {
  this.happiness + 20;
  
  }
  
  takesNap() {
   this.energy - 45;
   this.behavior + 15;
  }
}

var Charles = new Puppy(75, 25, 30);
Charles.getsTreat();
Charles.takesNap();

#2

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

Note: Backticks are not single quotes.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Your syntax is wrong.

For example, you are adding 20 to this.happiness but you are not assigning it to itself. It should be like

  getsTreat() {
  this.happiness = this.happiness + 20; // or
  this.happiness += 20;
  }

#4

Awesome, that fixed it. Thank you for your help!!