1

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)
Konrad
  • 21,590
  • 4
  • 28
  • 64
Matt P
  • 31
  • 2
  • 8
  • 1
    Seems that there may be something missing, your code runs fine both in the developer console and on StackOverflow's "run this snippet"? – joeybab3 Aug 15 '22 at 18:54
  • 1
    I don't understand what the issue is either – Konrad Aug 15 '22 at 18:55
  • this i why i am frustrated because in runs fine in VS code but when i submit the assignment it gives me the intern is not defined error – Matt P Aug 15 '22 at 19:09
  • 1
    It seems like the error comes from the JavaScript engine used by assignment authors. Nonetheless, I found only 2 unclear moments in your code: In `goToWork()` function, your are doing `this.xp + 10` instead of `this.xp += 10`. You are using `var` instead of `let` or `const`. – Akmal-threepointsix Aug 15 '22 at 19:31
  • resubmitted same code and it gave me a different error this time: Failed: Intern instance - returned: ,,TypeError,, but expected 10,10,Bob,21,110 Failed: Manager instance - returned: ,,TypeError,, but expected 100,30,Alice,30,110 – Matt P Aug 15 '22 at 19:32
  • 1
    changed var to both let and const and it kicked it back both times with Intern is not defined error. also changed the + to a +=. Thanks for the help so far – Matt P Aug 15 '22 at 19:37
  • 1
    The only way to reproduce the error was commenting out the line `var intern = new Woker(...)` ... you could try to use these comments :`/* Task 3 */` to make sure that for some stupid reason the line is not commented out. – HEllRZA Aug 15 '22 at 22:26
  • ok so adding the comment in front of the var intern = new worker () fixed the intern not defined error but still getting the Failed: Intern instance - returned: ,,TypeError,, but expected 10,10,Bob,21,110 Failed: Manager instance - returned: ,,TypeError,, but expected 100,30,Alice,30,110 It apparently thinks the results should be in a different order than what the assignment provided for in the directions. – Matt P Aug 17 '22 at 13:13

10 Answers10

1

Here is the solution for this problem.

// 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() {
       console.log(`I gained ${this.energy + 10} energy, cause I'm sleeping.`)
    }
    doSomethingFun() {
        console.log(`I still have ${this.energy - 10} energy let's keep dancing!`)
    }
}

// Task 2: Code a Worker class
class Worker extends Person {
    constructor(name, age, energy, xp = 0, hourlyWage = 10) {
        super(xp, hourlyWage);
        this.name = name
        this.xp = xp
        this.hourlyWage = hourlyWage
    }
    goToWork() {
        console.log(`${this.name}'s XP increased: ${this.xp + 10} points`)
    }
}

// Task 3: Code an intern object, run methods
function intern() {
    const inter1 = new Worker('Bob', 21, 110, 0, 10)
    inter1.goToWork()
    return inter1
}
console.log(intern())

// Task 4: Code a manager object, methods
function manager() {
    const manager1 = new Worker('Alice', 30, 120, 100, 30)
    manager1.doSomethingFun()
    return manager1
}
console.log(manager())
Relie Essom
  • 959
  • 9
  • 15
1

Here is the best approach to solving this kind of problem in javascript

// 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;
    }
    doSomethingFun(){
        this.energy -= 10;
    }
}

// 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;
    }
}

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

// Task 4: Code a manager object, methods
function manager() {
    const manager = new Worker(name="Alice", age=30, energy=120, xp=100, hourlyWage=30);
    manager.doSomethingFun();
    return manager;
}
0

    // 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();
  • Adding an explanation on how your code solves the OP's problem would improve your answer and make it more helpful to future visitors :) – Aaron Meese Oct 10 '22 at 20:02
0
// 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;
    }
    doSomethingFun(){
        this.energy-=10;
    }
}
// Task 2: Code a Worker class
class Worker extends Person{
    constructor(name,age,energy,xp=0,hourlyWage=10){
        super(name,age,energy);
        // this.name=name;
        this.xp=xp;
        this.hourlyWage=hourlyWage;
    }
    goToWork(){
        this.xp+=10;
    }
}
// Task 3: Code an intern object, run methods
function intern() {

    var intr= new Worker(name="Bob", age=21,energy=110,xp=0,hourlyWage=10)
    intr.goToWork()
    return intr;
}

// Task 4: Code a manager object, methods
function manager() {
    var intr2= new Worker(name="Alice", age=30,energy=120,xp=100,hourlyWage=30)
    intr2.doSomethingFun()
    return intr2;
}
0

check all of your spelling and brackets {} as i was going through it i double checked everything it worked after that

0
   It will work completely fine
 // 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;
    
        }
        dosomethingFun() {
            this.energy -= 10;
        }
    }
    // 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;
        }
    }
    // Task 3: Code an intern object, run methods
    function intern() {
        newIntern = new Worker("Bob", 21, 110, 0, 10);
        newIntern.goToWork();
        return newIntern;
    }
    
    // Task 4: Code a manager object, methods
    function manager() {
        newManager = new Worker("Alice", 30, 120, 100, 30);
        newManager.dosomethingFun();
        return newManager;
        
    }
    
    //calling functions to get the output
    console.log(intern());
    console.log(manager());
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you edit your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Juliano Alves Apr 10 '23 at 00:40
0
// 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;
        return(this.energy)
    }

    doSometingFun(){
        this.energy += 10;
        return (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;
        return (this.xp)
    }
    
}

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


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

[enter image description here][1]


  [1]: https://i.stack.imgur.com/OVQcA.png
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '23 at 05:04
-1

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

Matt P
  • 31
  • 2
  • 8
-1

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

Ben
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 06 '22 at 05:20
-1

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();