Passing function as a argument in Javascript

This is an example of how we can pass function as an argument in JavaScript.

 
script.js
 
var years = [1940, 1925,1957,2000,1928];
function arrayCal(arr, fn){
	var arrValues = [];
	for(var i =0; i < arr.length ; i++){
		arrValues.push(fn(arr[i]));
	}
	return arrValues;
}
function calAge(el){
	return 2016 - el;
}
function adultAge(el){
	return el >=18 ;
}
var ages = arrayCal(years, calAge)
console.log(ages);
var adult = arrayCal(ages, adultAge)
console.log(adult);
	

Leave a Reply

Your email address will not be published. Required fields are marked *