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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.