Created
September 8, 2014 03:06
-
-
Save RikkiGibson/02f762b28427210f9299 to your computer and use it in GitHub Desktop.
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
var li = [5,6,4,9,3]; | |
function min(a, b) { | |
return a < b ? a : b; | |
} | |
// reduce is an awesome function. in this situation it's doing: | |
// min(5,6) -> 5... min(5,4) -> 4... min(4,9) -> 4... min(4,3) -> 3... done | |
// you provide a function that takes two arguments and it goes through the list, | |
// using the return value as one of the arguments and the next list element as the other. | |
console.log(li.reduce(min)); // returns 3 | |
// procedural | |
// this took longer to write and I find its inner workings less expressive/self-documenting. | |
var min2 = li[0]; | |
for (var i = 0; i < li.length; i++) { | |
if (li[i] < min2) { | |
min2 = li[i]; | |
} | |
} | |
console.log(min2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment