I believe I have solved this problem but having an issue only passing 1 parameter to my final function to return result. Here is the question
我相信我已经解决了这个问题,但是只有一个参数传递给我的最终函数才能返回结果。这是个问题
For instance, consider the function getDouble. When run twice on value 3, yields 12 as shown below.
例如,考虑函数getDouble。当在值3上运行两次时,产生12,如下所示。
getDouble(3) => 6
getDouble(6) => 12
Let us name the new function createIterator and we should be able to obtain the same result using createIterator as shown below:
让我们将新函数命名为createIterator,我们应该能够使用createIterator获得相同的结果,如下所示:
var doubleIterator = createIterator(getDouble, 2); // This means, it runs *getDouble* twice
doubleIterator(3) => 12
For the sake of simplicity, all function inputs to createIterator would be functions returning a small number and number of iterations would always be integers.
为简单起见,createIterator的所有函数输入都是返回少量函数的函数,迭代次数总是整数。
Here is my answer:
这是我的答案:
function getDouble(num){
newNum = num * 2
return newNum
}
var createIterator = function (func, n) {
var results = [];
var result;
result = func;
results.push(func);
for(var i = 0;i < n - 1; i++){
results.push(results[i] * 2);
}
return results;
};
// var doubleIterator = createIterator(getDouble(3),2).slice(-1).pop();
Here is the function that I am trying to make to have only 1 call. Everything is working properly above but I must be able to submit this entire response only passing one parameter this this function:
这是我试图只进行一次调用的功能。一切都在上面正常工作但我必须能够提交这整个响应只传递一个参数这个函数:
function doubleIterator(d){
// var n;
// createIterator.call(this,n)
return createIterator(getDouble(d),n).slice(-1).pop();
}
console.log(doubleIterator);
1 个解决方案
#1
2
You could use this definition of createIterator:
您可以使用createIterator的这个定义:
function createIterator(func, n) {
return function (arg) {
for (var i = 0; i < n; i++) arg = func(arg);
return arg;
};
}
function getDouble(num){
return num * 2;
}
var doubleIterator = createIterator(getDouble, 2);
console.log(doubleIterator(3)); // => 12
You could also write the same with reduce
and bind
:
您也可以使用reduce和bind编写相同的内容:
function createIterator(func, n) {
return [].reduce.bind([...Array(n)], arg => func(arg));
}
function getDouble(num){
return num * 2;
}
var doubleIterator = createIterator(getDouble, 2);
console.log(doubleIterator(3)); // => 12
Although shorter code, it is a bit obscure. It binds this
and the first argument of reduce
as follows:
虽然代码较短,但有点模糊。它将此与reduce的第一个参数绑定如下:
-
this
: an empty array of n elements, which dictates how many times the callback function (i.e. the second argument, see below) is called. The actual values in the array are not important, as the callback function being used ignores them:this:一个由n个元素组成的空数组,它指示调用回调函数(即第二个参数,见下文)的次数。数组中的实际值并不重要,因为使用的回调函数会忽略它们:
-
callback function
arg => func(arg)
: will be called n times when thereduce
is actually invoked (which does not happen here yet). Although the callback function could accept the array value as second argument, there is no interest to do so.回调函数arg => func(arg):当实际调用reduce时将调用n次(这里还没有发生)。虽然回调函数可以接受数组值作为第二个参数,但是没有兴趣这样做。
The third argument is left unbound, and determines the initial value with which the callback function will be called the first time. So createIterator
returns a variant of reduce
that accepts only one argument, which is the initial value.
第三个参数保持未绑定状态,并确定第一次调用回调函数的初始值。因此createIterator返回reduce的变体,它只接受一个参数,即初始值。
Note that there is one harmless difference compared to the first snippet: if you call the function, that is returned by createIterator
, without argument, the function func is called one time less here (with undefined
as argument, just like in the first snippet); this is because of how reduce
works when you don't pass it an initial value.
请注意,与第一个片段相比,存在一个无害的区别:如果调用createIterator返回的函数,而不使用参数,则函数func在此处调用一次(使用undefined作为参数,就像在第一个片段中一样) ;这是因为当你没有传递初始值时,reduce如何工作。
#1
2
You could use this definition of createIterator:
您可以使用createIterator的这个定义:
function createIterator(func, n) {
return function (arg) {
for (var i = 0; i < n; i++) arg = func(arg);
return arg;
};
}
function getDouble(num){
return num * 2;
}
var doubleIterator = createIterator(getDouble, 2);
console.log(doubleIterator(3)); // => 12
You could also write the same with reduce
and bind
:
您也可以使用reduce和bind编写相同的内容:
function createIterator(func, n) {
return [].reduce.bind([...Array(n)], arg => func(arg));
}
function getDouble(num){
return num * 2;
}
var doubleIterator = createIterator(getDouble, 2);
console.log(doubleIterator(3)); // => 12
Although shorter code, it is a bit obscure. It binds this
and the first argument of reduce
as follows:
虽然代码较短,但有点模糊。它将此与reduce的第一个参数绑定如下:
-
this
: an empty array of n elements, which dictates how many times the callback function (i.e. the second argument, see below) is called. The actual values in the array are not important, as the callback function being used ignores them:this:一个由n个元素组成的空数组,它指示调用回调函数(即第二个参数,见下文)的次数。数组中的实际值并不重要,因为使用的回调函数会忽略它们:
-
callback function
arg => func(arg)
: will be called n times when thereduce
is actually invoked (which does not happen here yet). Although the callback function could accept the array value as second argument, there is no interest to do so.回调函数arg => func(arg):当实际调用reduce时将调用n次(这里还没有发生)。虽然回调函数可以接受数组值作为第二个参数,但是没有兴趣这样做。
The third argument is left unbound, and determines the initial value with which the callback function will be called the first time. So createIterator
returns a variant of reduce
that accepts only one argument, which is the initial value.
第三个参数保持未绑定状态,并确定第一次调用回调函数的初始值。因此createIterator返回reduce的变体,它只接受一个参数,即初始值。
Note that there is one harmless difference compared to the first snippet: if you call the function, that is returned by createIterator
, without argument, the function func is called one time less here (with undefined
as argument, just like in the first snippet); this is because of how reduce
works when you don't pass it an initial value.
请注意,与第一个片段相比,存在一个无害的区别:如果调用createIterator返回的函数,而不使用参数,则函数func在此处调用一次(使用undefined作为参数,就像在第一个片段中一样) ;这是因为当你没有传递初始值时,reduce如何工作。