This is the instruction 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.
Task 2: Code a Worker class
Code a sub-class, inheriting from the Person class, and name it Worker.
The Worker class has two additional parameters in the constructor:
- xp (for “experience points”)
- hourlyWage.
These properties are set to the following default values:
xp = 0
hourlyWage = 10
The Worker class has all the paramerters and methods of its super-class.
Additionally, it has the goToWork() method, which, whenever it’s run, increases the value of the xp property by 10.
and this is my code :
// Task 1: Code a Person class
class Person {
constructor(name = "Tom", age = 20, energy = 100) {
this.name = name;
this.age = age;
this.energy = energy;
}
sleep() {
this.energy += 20;
console.log('Energy is increasing, currently at:', this.energy)
}
doSomethingfun() {
this.energy -= 20;
console.log('Energy is decreasing, currently at:', this.energy)
}
}
// Task 2: Code a Worker class
class Worker extends Person{
constructor(xp = 0, hourlyWage = 10, name, age, energy) {
super(name, age, energy);
this.xp = xp;
this.hourlyWage = hourlyWage;
}
goToWork () {
if (this.xp > 0) {
this.xp = + 10;
console.log('xp is decreasing, currently at:', this.xp)
}
}
}
and i still get an error and i don’t know why ![]()
the error message:
Your code could not be executed. Error:TypeError: Cannot read property ‘xp’ of undefined
at testInstance (/home/coder/project/autograde/grader.js:24:37)
at courseraRunTestCases (/home/coder/project/autograde/grader.js:40:5)
at Object. (/home/coder/project/autograde/grader.js:47:5)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
at internal/main/run_main_module.js:17:47