Last active
June 23, 2017 05:17
-
-
Save edsfocci/ae67b513fb3b5091c8f303fabb9aa00c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// From http://csbin.io/callbacks | |
// Video: https://www.youtube.com/watch?v=py8gaw6DXRM | |
// Slides: https://drive.google.com/file/d/0B44cfJmZKU0hQnRIRkpzNi1CWW8/view | |
// Type JavaScript here and click "Run Code" or press Ctrl + s | |
console.log('Hello, world!'); | |
// Challenge 1 | |
function addTwo(num) { | |
return num + 2; | |
} | |
// To check if you've completed it, uncomment these console.logs! | |
// console.log(addTwo(3)); | |
// console.log(addTwo(10)); | |
// Challenge 2 | |
function addS(word) { | |
return word + 's'; | |
} | |
// uncomment these to check your work | |
// console.log(addS('pizza')); | |
// console.log(addS('bagel')); | |
// Challenge 3 | |
function map(array, callback) { | |
var result = []; | |
for (var i = 0; i < array.length; i++) { | |
result.push(callback(array[i])); | |
} | |
return result; | |
} | |
// console.log(map([1, 2, 3], addTwo)); | |
// Challenge 4 | |
function forEach(array, callback) { | |
for (var i = 0; i < array.length; i++) { | |
callback(array[i]); | |
} | |
} | |
// see for yourself if your forEach works! | |
// var alphabet = ''; | |
// var letters = ['a', 'b', 'c', 'd']; | |
// forEach(letters, function(char) { | |
// alphabet += char; | |
// }); | |
// console.log(alphabet); | |
//-------------------------------------------------- | |
// Extension | |
//-------------------------------------------------- | |
//Extension 1 | |
function mapWith(array, callback) { | |
var result = []; | |
forEach(array, function(item) { | |
result.push(callback(item)); | |
}); | |
return result; | |
} | |
// console.log(mapWith([1, 2, 3], addTwo)); | |
//Extension 2 | |
function reduce(array, callback, initialValue) { | |
var result = initialValue; | |
forEach(array, function(item) { | |
result = callback(result, item); | |
}); | |
return result; | |
} | |
// var nums = [4, 1, 3]; | |
// var add = function(a, b) { return a + b; } | |
// console.log(reduce(nums, add, 0)); | |
//Extension 3 | |
function intersection(arrays) { | |
var arrays = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)); | |
if (arrays.length <= 1) return []; | |
return reduce(arrays.slice(1, arrays.length), function(arr1, arr2) { | |
var result = []; | |
for (var i = 0; i < arr2.length; i++) { | |
if (arr1.indexOf(arr2[i]) >= 0) { | |
result.push(arr2[i]); | |
} | |
} | |
return result; | |
}, arrays[0]); | |
} | |
// console.log(intersection([5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20])); | |
// should log: [15, 5] | |
//Extension 4 | |
function union(arrays) { | |
var arrays = (arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)); | |
if (arrays.length === 0) return []; | |
if (arrays.length === 1) return arrays[0]; | |
return reduce(arrays.slice(1, arrays.length), function(arr1, arr2) { | |
var result = arr1.slice(); | |
for (var i = 0; i < arr2.length; i++) { | |
if (result.indexOf(arr2[i]) === -1) { | |
result.push(arr2[i]); | |
} | |
} | |
return result; | |
}, arrays[0]); | |
} | |
// console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5])); | |
// should log: [5, 10, 15, 88, 1, 7, 100] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment