最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiply (a)(b) function possible? - Stack Overflow

programmeradmin2浏览0评论

I have an oddball of a question from a code quiz. They expect me to write a function that multiplies (a) and (b) but instead of writing it like:

  multiply(a,b) {
    return a*b;
  }

They expect me to do the math with:

    multiply(a)(b)

Is it possible ?

I have an oddball of a question from a code quiz. They expect me to write a function that multiplies (a) and (b) but instead of writing it like:

  multiply(a,b) {
    return a*b;
  }

They expect me to do the math with:

    multiply(a)(b)

Is it possible ?

Share Improve this question edited Jun 11, 2022 at 14:12 Nikola Lukic 4,2586 gold badges48 silver badges80 bronze badges asked Mar 26, 2018 at 0:08 OjoOjo 451 gold badge1 silver badge8 bronze badges 1
  • 3 this is called currying – A. L Commented Mar 26, 2018 at 0:09
Add a ment  | 

3 Answers 3

Reset to default 4

Make a function that returns another function.

const multiply = a => b => a * b;
console.log(multiply(4)(3));

Looks like they'd like you to use currying. Currying allows you to reduce functions of more than one argument to functions of one argument. The link below has a very similar example to what your employer is looking for.

Helpful Resource: https://blog.benestudio.co/currying-in-javascript-es6-540d2ad09400

I hope this answers your question.

It's a function that returns a function

function multiply (a) {
  return function (b) {
    return a * b;
  };
};

console.log(multiply(4)(3));
发布评论

评论列表(0)

  1. 暂无评论