How to modify number values in a class constructor

I am really confused as to why my code is not working. Here is the problem and my code below.
----------------------------------------------------------
The class Puppy needs two methods.

The getsTreat method should accept one parameter. It should increase happiness by 20 for each treat received, 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.
--------------------------------------------------------
class Puppy {

  constructor(happiness, energy, behavior) {
    this.happiness = happiness
    this.energy = energy
    this.behavior = behavior
  }
 
  // code here
  getsTreat(num){
    return this.happiness + num * 20;
  }
 
  takesNap(){
    this.energy - 45;
    this.behavior + 15;
  }
}

var Charles = new Puppy(75, 25, 30)
Charles.takesNap()
Charles.getsTreat(4)
Charles
matt wrightAsked:
Who is Participating?
 
Nitin SontakkeDeveloperCommented:
The reason it is not working is you are just making calculations and not assigning results back to the variables at all.

Try as follows, instead:

class Puppy {

  constructor(happiness, energy, behavior) {
    this.happiness = happiness
    this.energy = energy
    this.behavior = behavior
  }
  
  // code here
  getsTreat(num){
	this.happiness += (num * 20);
    return this.happiness;
  }
  
  takesNap(){
    this.energy -= 45;
    this.behavior += 15;
  }
}

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

Open in new window

0
 
hieloCommented:
class Puppy {

  constructor(happiness, energy, behavior) {
    this.happiness = happiness
    this.energy = energy
    this.behavior = behavior
  }
  
  // code here
  getsTreat(num){
    // you can either assign it directly with the = operator
    // this.happiness = this.happiness + num * 20;

    // or you can also use the += operator
    this.happiness += (num * 20);
return this.happiness
  }
  
  takesNap(){
    this.energy -= 45;
    this.behavior += 15;
  }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.