[英]write a twice function that multiplies one number four times by itself in the format below
I want to write a function called twice that takes a function f and a value x as its parameters and returns f(f(x))
. 我想编写一个调用两次的函数,该函数将函数f和值x作为其参数并返回f(f(x))
。 For example, 例如,
twice(function (x) { return x * x; }, 3)
should return 81
. 应该返回81
。 How do I do it? 我该怎么做?
Call the anonymous function twice which is passed as an argument. 调用两次匿名函数作为参数传递。 On the second call use the returned value of first execution as the argument. 在第二次调用中,将第一次执行的返回值用作参数。
function twice(funct, a) { return funct(funct(a)); } console.log( twice(function(x) { return x * x; }, 3) );
What is stopping you? 什么阻止了你? You already has the answer with you. 您已经有了答案。 It is working pretty fine. 运行正常。
function twice(f, x) { return f(f(x)); } var result = twice(function(n){return n*n;}, 3); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.