简体   繁体   English

高阶 function

[英]Higher-order function

I have an exercise about JavaScript.我有一个关于 JavaScript 的练习。 This exercise requires me to use higher-order functions.这个练习需要我使用高阶函数。 I have managed to specify some of the functions so far, but when I try to execute the code, the result does not seem to work properly.到目前为止,我已经设法指定了一些函数,但是当我尝试执行代码时,结果似乎无法正常工作。 I have some images to give you an idea, hopefully, you can help me correct this.我有一些图片可以给你一个想法,希望你能帮助我纠正这个问题。

  • The thread is: Write the function loop(loops, number, func), which runs the given function the given number of times.线程是:编写 function loop(loops, number, func),它运行给定的 function 给定的次数。 Also write the simple functions halve() and square().还要编写简单的函数 halves() 和 square()。

  • This is my code:这是我的代码:

 function loop(loops, number, func) { var loops = function(n) { for (var i = 0; i < n; i++) { if (i < 0) { console.log('Programme ended') } if (i > 0) { return n; } } } } var halve = function(n) { return n / 2 } var square = function(n) { return n ** 2; } console.log(halve(50)); console.log(loop(5, 200, halve)); console.log(loop(3, 5, square)); console.log(loop(-1, 99, halve));

在此处输入图像描述

Your current loop function declares an inner function and then exits.您当前的loop function 声明了一个内部 function 然后退出。 Ie, nothing actually happens -即,实际上什么也没发生——

function loop(loops,number,func){

    // declare loops function
    var loops= function(n){
         // ...
    }

    // exit `loop` function
}

One such fix might be to run the supplied func a number of times in a for loop, like @code_monk suggest.一种这样的解决方法可能是在for循环中多次运行提供的func ,例如 @code_monk 建议。 Another option would be to use recursion -另一种选择是使用递归 -

 function loop (count, input, func) { if (count <= 0) return input else return loop(count - 1, func(input), func) } function times10 (num) { return num * 10 } console.log(loop(3, 5, times10)) // 5000

Here's one way这是一种方法

 const loop = (loops, n, fn) => { for (let i=0; i<loops; i++) { console.log( fn(n) ); } }; const halve = (n) => { return n / 2; }; const square = (n) => { return n ** 2; }; loop(2,3,halve); loop(4,5,square);

so first things first: Higher-Order functions are functions that work on other functions.所以首先要做的事情是:高阶函数是作用于其他函数的函数。 The reason why you get undefined is because you are calling a function which doesn't return anything.你得到 undefined 的原因是因为你正在调用一个 function ,它不返回任何东西。

function x(parameter){
  result = parameter + 1;
}
// -> returns undefined every time
console.log(x(5));
// -> undefined
function y(parameter){
  return parameter+1;
}
// -> returns a value that can be used later, for example in console.log
console.log(y(5));
// -> 6

Second, you are using n for your for loop when you should probably use loops so it does the intended code as many times as "loops" indicates instead of the number you insert (ie 200, 5, 99).其次,当您可能应该使用循环时,您将 n 用于您的for 循环,因此它执行预期代码的次数与“循环”指示的次数一样多,而不是您插入的数字(即 200、5、99)。

By having the "console.log" inside a loop you may get a lot of undesired "programme ended" in your output so in my version I kept it out of the loop.通过在循环中包含“console.log”,您可能会在 output 中获得很多不需要的“程序结束”,因此在我的版本中,我将其排除在循环之外。

The other two answers given are pretty complete I believe but if you want to keep the for loop here goes:我相信给出的其他两个答案非常完整,但如果你想在这里保留for 循环

function loop(loops, number, func){
  if(loops>0){
    for(let i = 0; i< loops; i++){     // let and const are the new ES6 bindings (instead of var)
      number = func(number)
    }
    return number
  }
  else{
    return "Programme ended"
  }
}

function halve(n) {     // maybe it's just me but using function declarations feels cleaner
    return n / 2;
}
  
function square(n) {    
    return n ** 2;
}

console.log(halve(50));
console.log(loop(5, 200, halve));
console.log(loop(3, 5, square));
console.log(loop(-1, 99, halve));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM