简体   繁体   English

实现一个名为“processLastItem”的高阶函数

[英]Implement a higher-order function called `processLastItem`

I'm working on homework and I'm completely lost.我正在做家庭作业,我完全迷失了。
This is the question:这是问题:

Implement a higher-order function called processLastItem .实现一个名为processLastItem的高阶函数。 It takes two arguments:它需要两个参数:

  • @param stringList array of strings. @param stringList 字符串数组。
  • @param callback function that takes a string as its argument. @param 以字符串为参数的回调函数。
  • @returns the result of invoking callback with the LAST element in stringList ." @Returns调用的结果, callback在最后一个元素stringList “。

They're throwing this at us without having taught it yet so I have no idea where to even start, can someone show me at least where to begin?他们在没有教过它的情况下就把它扔给我们,所以我什至不知道从哪里开始,有人能告诉我至少从哪里开始吗?

A higher order function is basically a function that either returns another function as its result, or takes in a function as a parameter.高阶函数基本上是一个函数,它要么返回另一个函数作为其结果,要么接受一个函数作为参数。 Here's an example of an implementation of forEach , which is a higher order function.这是forEach的一个实现示例,它是一个高阶函数。 This is pretty useless for actual production code, but hopefully illustrates the concept:这对于实际的生产代码非常无用,但希望能说明这个概念:

function forEach(array, callbackFunction) {
    for (const item of array) {
        // note we are now calling the passed in function
        callbackFunction(item);
    }
}

So what you need to do with your homework is take this example and pair that together with code to get the last element of an array.所以你需要做的功课就是拿这个例子,把它和代码结合起来,得到数组的最后一个元素。 Hopefully that shouldn't be as hard with a clear example.希望通过一个明确的例子,这不应该那么难。

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

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