I get a challenge to build OOP in javascript, and i don’t know how to do with method in classes part, This is the task that i working on :
Task 1: Code a Person class
Code a Person class, with three parameters in the constructor: name, age, and energy.
Set the default parameters in the Person class as follows:
name = "Tom"
age = 20
energy = 100
Code two methods in the Person
class. Name those methods sleep()
and doSomethingFun()
.
The sleep()
method should take the existing energy level and increase it by 10.
The doSomethingFun() method should take the existing energy level and decrease it by 10.
and this is my code so far :
// Task 1: Code a Person class
class Person{
constructor(name,age,energy) {
this.name = name;
this.age = age;
this.energy = energy;
}
sleep() {
}
doSomethingfun() {
}
}