简体   繁体   English

Javascript 分配有问题,我不知道解决方案是什么,我已经尝试了一切

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

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.我是编程新手,我有一个作业不断抛出 (Your code could not be executed. Error:ReferenceError: intern is not defined ) 错误。

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.说明是任务 3:编码实习生 object 在实习生 function 中实例化 Worker class 以编码新实习生 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.对实习生 object 运行goToWork()方法。然后返回实习生 object。

Task 4: Code a manager object Inside the manager function instantiate the Worker class to code a new manager object.任务 4:编写经理 object 在经理 function 中实例化 Worker class 以编写新经理 object。

The manager object should have the following characteristics:经理object应该具备以下特征:

name: Alice

age: 30

energy: 120

xp: 100

hourlyWage: 30

Run the doSomethingFun() method on the manager object. Then return the manager object.在管理器object上运行doSomethingFun()方法。然后返回管理器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)

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

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.您只需要返回实习生和经理 object。 The console.log code not required.不需要 console.log 代码。

Here is the complete solution with a 100% passing grade.这是具有 100% 及格率的完整解决方案。

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

Thanks man, this is ridiculous.谢谢大佬,这太荒谬了。 I can't believe I'm paying for this course and it clearly isn't ready for use.我不敢相信我正在为这门课程付费,而且它显然还没有准备好使用。 It seems like nobody has tested it, or went into the course as a student and actually tried to follow their instructions and pass these things.似乎没有人测试过它,或者作为学生进入课程并实际上尝试按照他们的指示并通过这些东西。 It's like they did the absolute bare minimum.就像他们做了绝对的最低限度。 This is the second lab in a row to have all of these problems.这是连续第二个遇到所有这些问题的实验室。

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

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

check all of your spelling and brackets {} as i was going through it i double checked everything it worked after that检查你所有的拼写和括号{},因为我正在检查它我仔细检查了所有它之后的工作

Here is the best approach to solving this kind of problem in javascript这是解决此类问题的最佳方法 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 分配有问题:数组和 object 迭代但我不知道解决方案是什么,我已经尝试了一切 - Having a problem with a Javascript assignment: Array and object iteration but I have no idea what the solution is, I have tried everything 我在 HTML 中对 forms 的这个想法/问题最接近的解决方案是什么? - What is the closest solution to this Idea/Problem I have with forms in HTML? 我不知道这段代码是什么 - I have no idea what this code is 此javascript代码在没有for循环的情况下可以正常运行,但是没有循环。任何帮助,我都尝试了一切 - This javascript code is running properly without the for loop but not with it.Any help, I have tried everything 我遇到了 Discord.js 代码的问题,我没有找到解决方案 - I am having a problem with Discord.js code I have not found a solution 我在javascript中遇到问题 - I have a problem with a function in javascript 我对 javaScript 中的 if 语句有疑问 - I have a problem with if statement in javaScript 我正在尝试打开 React Native 应用程序,但我遇到了这个问题,解决办法是什么? - I am trying to open the React Native application but I have this problem What is the solution? 我想用@keyframes 将 animation 添加到 div 中。 我基本上已经尝试了我所知道的一切 - i want to add an animation to the div with @keyframes. I have tried basically everything i know 这是我遇到了一段时间的错误“找不到模块:无法解析'../lib'”,不知道问题出在哪里 - This is an error i have been encountering for a while now "Module not found: Can't resolve '../lib'", no idea what the problem is
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM