Skip to content

Instantly share code, notes, and snippets.

@JasonDeving
Created March 20, 2016 02:58
Show Gist options
  • Save JasonDeving/c966483ccf5df6be5618 to your computer and use it in GitHub Desktop.
Save JasonDeving/c966483ccf5df6be5618 to your computer and use it in GitHub Desktop.
Callbacks
Write a function, funcCaller, that takes a func (a function) and an arg (any data type). The function returns the func called with arg(as an argument).
var funcCaller = function(func, arg) {
return func(arg);
}
Write a function, firstVal, that takes an array, arr, and a function, func, and calls func with the first index of the arr, the index # and the whole array.
var firstVal = function(arr, func){
return func(arr[0], 0, arr);
}
Change firstVal to work not only with arrays but also objects. Since objects are not ordered, you can use any key-value pair on the object.
var firstVal = function(list, func) {
if(Array.isArray(list)) {
return func(arr[0], 0, arr);
} else {
for(var k in list){
return func(list[k], k, list);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment