Skip to content

Instantly share code, notes, and snippets.

@nvurgaft nvurgaft/index.html
Created Aug 15, 2015

Embed
What would you like to do?
Examples for using anonymouse functions with functional programming in Javascript
<div>
<p id="text1"></p>
<p id="text2"></p>
</div>
function forEach(array, fn) {
var result = [];
for (var i=0; i<array.length; i++) {
result.push(fn(array[i]));
}
return result;
}
var data = ['hello', 'ms', 'robinson'];
var iteratedData = forEach(data, function(item) {
return item.length;
})
document.getElementById('text1').innerHTML = iteratedData;
function filter(array, fn) {
var result = [];
for (var i=0; i<array.length; i++) {
if (fn(array[i])) {
result.push(array[i]);
}
}
return result;
}
var datum = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var newData = filter(datum, function(item) {
if (item%3===0) {
return true;
}
});
document.getElementById('text2').innerHTML = newData;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.