[JS Practice] execute multiple functions successively on a value

Title :

Execute multiple functions successively on a value

Description:

Write a generic function chainer that takes a starting value, and an array of functions to execute on it. Return the final value after execution is complete.

Example

function add(num) { 
   return num + 1 
} 

function mult(num) { 
   return num * 30 
} 

chain(2, [add, mult]); // returns 90;

My solution :

function chain(input, fs) {
 var ret = input;
 for(var i=0; i<fs.length; i++) {
 ret = fs[i](ret);
 }
 return ret;
}

Ref. from codewars

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s