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
function factorial(n) { | |
var result = 1; | |
while (n>1) { | |
result = result * n; | |
n = n - 1; | |
} | |
return result; | |
} |
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
// Calculate the nth Fibonacci number: imperative style | |
function fibonacci(n) { | |
var a = 0, b = 1, sum = 0; | |
if (n<0) return NaN; | |
if (n<2) return n; | |
while (n>1) { | |
sum = a + b; | |
a = b; |
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
// Here are the kids... | |
var kids = [{name:"Abigail", isNaughty:false}, | |
{name:"Ben", isNaughty:false}, | |
{name:"Clara", isNaughty:true}, | |
{name:"David", isNaughty:false}, | |
{name:"Emily", isNaughty:true}, | |
{name:"Fred", isNaughty:false}, | |
{name:"Gloria", isNaughty:false}, | |
{name:"Harry", isNaughty:false}, | |
{name:"Ingrid", isNaughty:true}, |
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
function(k) { | |
return !k.isNaughty; | |
} |
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
k => !k.isNaughty |
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
// Find the good ones... | |
var good_kids = kids.filter(k => !k.isNaughty).length; |
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
(acc,k) => acc + !k.isNaughty |
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 nice_kids = kids.reduce((acc,k) => acc + !k.isNaughty,0); |
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 naughty_kids = kids.reduce((acc,k) => acc + k.isNaughty,0); |
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 naughty_and_nice = kids.reduce((acc,k) => { | |
acc.nice += !k.isNaughty; | |
acc.naughty += k.isNaughty; | |
return acc; | |
}, {naughty:0, nice:0}); |
OlderNewer