简体   繁体   English

编写一个double函数,将其自身乘以4,格式如下

[英]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.

相关问题 JavaScript函数可产生一个数组,该数组根据前一个整数乘以x倍 - JavaScript function that produces an array that multiplies x number of times based on previous integer 函数式组件渲染四次——为什么不渲染两次? - Functional component renders four times - why not twice? 如何编写 function 以以下格式给出 output? - How can I write a function which gives the output in the below format? js 函数调用两次(一个在文档准备好,一个在添加新的 dom 元素之后)执行两次 - js function called twice (one in document ready, one after adding new dom element) executing twice times Javascript:编写一个接受数字的函数,并多次返回带有该数字的数组 - Javascript: Write a function that takes in a number, and returns an array with that number in it that many times 数字转换为十进制格式,并附加四个十进制 - Number to Decimal Format with Four Decimal extra 将数组相乘的DRYing函数 - DRYing Function that multiplies array 编写一个函数来抛硬币并返回正面朝上的次数? - Write a function that flips a coin and returns the number of times landed heads? 创建一个可重复x次的字符串 - Create a string that repeats itself x number of times 如何让 function 调用自身 n 次 - How to make a function call itself n times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM