Currified function that multiples array elements in JavaScript


Problem

We are required to write a JavaScript function that takes in an array and returns another function which in turn takes in a number which returns a new array which is the product of corresponding elements of the input array to the first function and the number provided to the second function.

Example

Following is the code −

 Live Demo

const arr = [2, 5, 2, 7, 8, 4];
const num = 4;
const produceWith = (arr = []) => (num) => {
   const res = arr.map(el => {
      return el * num;
   });
   return res;
};
console.log(produceWith(arr)(num));

Output

Following is the console output −

[ 8, 20, 8, 28, 32, 16 ]

Updated on: 17-Apr-2021

130 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements