Tuesday, September 14, 2021

Example of function taking function as arguments.

 var year=[1990,1965,1970,1980];

function arrayCalc(arr,fn){

    var res=[];

    for(var i=0;i<arr.length;i++){

        res.push(fn(arr[i]))

    }

    return res;

}

function calculateAge(el){

    return 2021-el;

}

var result=arrayCalc(year,calculateAge);//here calculateAge is the callback function .

console.log(result);


No comments:

Post a Comment

Clean code chapter 3(Robert C.martin)

  Summary: --------  1. Functions should hardly ever be 20 lines long.  2.Keep blocks (inside if, else, while, for, etc.) short.  Ideally, j...