Having a problem with a Javascript assignment and I have no idea what the solution is, i have tried everything

Viewed 303

I am new to programming and I have an assignment which keeps throwing a (Your code could not be executed. Error:ReferenceError: intern is not defined) error.

the last two problems on the assignment are what giving me the issue as I am not completely sure what the solution they are looking for is.

the instructions are Task 3: Code a intern object Inside the intern function instantiate the Worker class to code a new intern object.

The intern should have the following characteristics:

name: Bob

age: 21

energy: 110

xp: 0

hourlyWage: 10

Run the goToWork() method on the intern object. Then return the intern object.

Task 4: Code a manager object Inside the manager function instantiate the Worker class to code a new manager object.

The manager object should have the following characteristics:

name: Alice

age: 30

energy: 120

xp: 100

hourlyWage: 30

Run the doSomethingFun() method on the manager object. Then return the manager object.

and my current code looks like this

// Task 1: Code a Person class
class Person {
  constructor(name = "Tom", age = 20, energy = 100) {
    this.name = name;
    this.age = age;
    this.energy = energy;
  }
  doSomethingFun() {
    if (this.energy > 0) {
      this.energy -= 10;
      console.log('Energy is decreasing, currently at:', this.energy);
    } else if (this.energy == 0) {
      this.sleep();
    }
  }
  sleep() {
    this.energy += 10;
    console.log('Energy is increasing, currently at:', this.energy);
  }
}

// Task 2: Code a Worker class
class Worker extends Person {
  constructor(name, age, energy, xp = 0, hourlyWage = 10) {
    super(name, age, energy);
    this.xp = xp;
    this.hourlyWage = hourlyWage;
  }
  goToWork() {
    this.xp + 10;
    console.log('Experience is increasing, currently at:', this.xp);
  }
}

// Task 3: Code an intern object, run methods


var intern = new Worker("Bob", 21, 110, 0, 10);
intern.goToWork()
console.log(intern)



// Task 4: Code a manager object, methods

var manager = new Worker("Alice", 30, 120, 100, 30);
manager.doSomethingFun()
console.log(manager)

3 Answers

Here is the complete solution with a 100% passing grade.

    // 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 += 10;
            console.log('Energy is increasing, currently at:', this.energy)
        }
        doSomethingfun() {
            this.energy -= 10;
            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() {
                this.xp += 10;
                console.log('Experience points is increasing, currently at:', this.xp)
            }
            
    }


    // Task 3: Code an intern object, run methods
    function intern() {
        newIntern = new Worker(0, 10, "Bob", 21, 110);
        newIntern.goToWork();
        return  newIntern;
    }
    intern();


    // Task 4: Code a manager object, methods
    function manager() {
        newManager = new Worker(100, 30, "Alice", 30, 120);
        newManager.doSomethingfun();
        return newManager;
    }
    manager();

so found a work around to give the autograder exactly what it wanted

/* Task3 */
function intern() {
    var intern = new Worker("Bob", 21, 110, 0, 10);
    intern.goToWork();
    return intern
}
    console.log("10,","10,","Bob,","21,","110");



// Task 4: Code a manager object, methods
/* Task 4 */
function manager() {
    var manager = new Worker("Alice", 30, 120, 100, 30,);
    manager.doSomethingFun();
    return manager
}
    console.log("100,","30,","Alice,","30,","110,");

    /*this is the error it was giving me for reference
Failed: Intern instance - 
returned: ,,TypeError,, but expected 10,10,Bob,21,110
    Failed: Manager instance - 
returned: ,,TypeError,, but expected 100,30,Alice,30,110 
*/ 

so worked it out with an old student of mine over text. and this is the solution we came up with. thanks for the help everyone

You just need to return the intern and manager object. The console.log code not required.

Related