我的任务是创建一个算术任务运行器,作为我分配的一部分。
到目前为止,我从未使用过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.
如果能获得任何帮助,我将不胜感激。
我遇到的问题如下:execute方法(试图使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
答案 0 :(得分:1)
我不想提供完整的答案,因为这是给您的任务,但是下面的一些代码可能会对您有所帮助。这从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))
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));