Anthony Boyac Anthony Boyac - 16 days ago 5
Javascript Question

write a twice function that multiplies one number four times by itself in the format below

I want to write a function called twice that takes a function f and a value x as its parameters and returns

f(f(x))
. For example,

twice(function (x) { return x * x; }, 3)


should return
81
. How do I do it?

Answer

What is stopping you? You already has the answer with you. It is working pretty fine.

function twice(f, x) {
  return f(f(x));
}

var result = twice(function(n){return n*n;}, 3);

console.log(result);