gpt4 book ai didi

javascript - 创建算术任务运行器

转载 作者:行者123 更新时间:2023-12-03 00:47:13 25 4
gpt4 key购买 nike

作为我的作业的一部分,我的任务是创建一个算术任务运行程序。

直到今天我还从未使用过 NodeJS,甚至从未使用过终端来执行脚本。

过去 5 个小时我一直在研究这个问题,但仍然没有成功。我避免来这里询问,因为我想自己解决这个问题,但是,我已经屈服于迫切需要帮助。

这是我到目前为止的代码:

class ArithmeticTaskRunner {
static set taskCount(counter) {
throw new('This is a readOnly accessor, the value is ${value}');
}

add(y) {
this.y = y || 0
console.log(y)
}

minus(x) {
this.x = Math.abs(this.y) * -1;
console.log(this.x);
};

multiply(z) {
this.z = z * this.x;
console.log(this.z)
}

execute(startValue) {
this.startValue = startValue + this.y
this.y = this.startValue
console.log(this.startValue)
this.startValue = this.minus
console.log(this.startValue)
this.startValue = this.multiply(this.startValue)
console.log(this.startValue)
}
}

tasks = [
function() { minus()},
function() { multiply(z)},
function() { add(x)},
function() { execute(x)}
]

这远非完美,但已接近完成 80%-90%。

这是我被赋予的任务:

You should implement a class called ArithmeticTaskRunner with the following:
- An instance variable named tasks which is initialised to an empty array upon
creation.
- A method named addNegationTask which adds an anonymous function to the
tasks array. This anonymous function should take one argument, x, and return the
negation, -x.
- A method named addAdditionTask which takes a single argument y, and adds
an anonymous function to the tasks array. This anonymous function should take
one argument, x, and return the result x + y.
- A method named addMultiplicationTask which takes a single argument y,
and adds an anonymous function to the tasks array. This anonymous function
should take one argument, x, and return the result x * y.
- A read-only accessor named taskCount which returns the number of queued tasks.
- A method named execute, which takes a single argument named startValue.
If omitted, startValue defaults to zero. Starting at startValue, this method should iterate over the tasks array executing each function on the current value. It then returns the resulting number after all arithmetic operations have been executed.

如果我能得到任何帮助,我将不胜感激。

我遇到的问题如下:执行方法(尝试使 startValue,在加法之后为负数)、乘法方法以及事实上我无法在不调用加法方法两次的情况下调用加法方法覆盖该值。程序完全运行的示例表明我应该允许多次调用一个方法而不覆盖以前的值。

我知道有一条规则,每一期只有一个问题,我承认这一点。但如果有人能帮助我解决任何问题,我将非常感激,并且我将补偿人们的努力。

谢谢。

编辑 - 这是预期输入/输出的示例

> let taskRunner = new ArithmeticTaskRunner()
undefined
> taskRunner.addAdditionTask(2)
undefined
> taskRunner.addMultiplicationTask(4)
undefined
> taskRunner.addAdditionTask(10)
undefined
> taskRunner.execute(2)
26
> taskRunner.execute(-2)
10

最佳答案

我不想提供完整的答案,因为这是给您的作业,但这里有一些代码可能会帮助您。从 5 开始,然后调用 doubleIt,然后调用 addOne 到达 11

它通过创建一个函数数组来实现这一点,每个函数执行一项简单的任务并返回以某种方式修改其输入的结果。

然后它创建一个名为 execute 的函数,该函数使用 Array.reduce 使用给定的初始值调用数组中的第一个函数,然后根据结果重复调用数组中的每个函数。检查 Array.reduce 的文档如果您对其工作原理感到困惑。

doubleIt = x => x * 2;
addOne = x => x + 1;
tasks = [doubleIt, addOne];
execute = (initial) => tasks.reduce((x,fn) => fn(x), initial)
document.write(execute(5))

提示#2

class ArithmeticTaskRunner {
constructor() {
this.tasks = [];
}

addAdditionTask(arg) {
this.tasks.push(x => x + arg);
}

addMultiplicationTask(arg) {
this.tasks.push(x => x * arg);
}

execute(startValue) {
return this.tasks.reduce((x, fn) => fn(x), startValue);
}
}

let taskRunner = new ArithmeticTaskRunner()
taskRunner.addAdditionTask(2)
taskRunner.addMultiplicationTask(4)
taskRunner.addAdditionTask(10)
document.write(taskRunner.execute(2));
document.write(', ');
document.write(taskRunner.execute(-2));

关于javascript - 创建算术任务运行器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53197043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com