In functional programming, how to convert a function which uses global variables into a pure function?

39 views Asked by At

In the code below, power isn't a pure function (uses two global variables), how can i convert it into a pure function with just one input array as parameter?

const multiplyByFour = function (array) {
  return array.map((item) => item * 4);
};

const sumElements = function (array) {
  return array.reduce((total, num) => {
    return total + num;
  });
};

function power(arr) {
  let lengthArray = arr.length;
  let value = sumElements(multiplyByFour(arr));
  return Math.pow(value, lengthArray);
}

console.log(power([3, 4, 2]));
0

There are 0 answers