Help me ! I'm stuck when did some OOP in Javascript

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 :disappointed_relieved:
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

Hey! can you provide the context in which you’re getting this error because i don’t get this error when i declare a new worker on my computer

i don’t get an error in the terminal too bro but the result of my challenge said like this :

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

i just confused which part i did wrong, because my code didn’t get any error in the console

can you please add a link to the challenge?

you can’t login because this is in coursera challenge bro

yeah, maybe you can try asking the instructors or the students who are enrolled in the course because i don’t know the context in which this code is running. It seems to work for me so far.

yeah maybe i can try once again, can we just contact in message later?
i just need some friend to share each other

Sure! let me know how i can be of help.

Here is the solution for this problem.

Mod Edit: SOLUTION REMOVED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.