Created
March 20, 2016 02:58
-
-
Save JasonDeving/c966483ccf5df6be5618 to your computer and use it in GitHub Desktop.
Callbacks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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