Skip to content

Instantly share code, notes, and snippets.

View ChrisWhealy's full-sized avatar
🏠
Working from home (funnily enough...)

Chris Whealy ChrisWhealy

🏠
Working from home (funnily enough...)
View GitHub Profile
@ChrisWhealy
ChrisWhealy / factorial_imp.js
Created April 28, 2016 15:35
Imperative implementation of the factorial function
function factorial(n) {
var result = 1;
while (n>1) {
result = result * n;
n = n - 1;
}
return result;
}
@ChrisWhealy
ChrisWhealy / fibonacci_imp.js
Last active September 25, 2022 19:33
Imperative implementation of the Fibonacci function
// 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;
@ChrisWhealy
ChrisWhealy / mindshift03_01.js
Created April 28, 2016 15:44
Mindshift: Part 3, sample 1
// 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},
@ChrisWhealy
ChrisWhealy / mindshift03_02.js
Created April 28, 2016 15:45
Mindshift: Part 3, sample 2
function(k) {
return !k.isNaughty;
}
@ChrisWhealy
ChrisWhealy / mindshift03_03.js
Created April 28, 2016 15:46
Mindshift: Part 3, sample 3
k => !k.isNaughty
@ChrisWhealy
ChrisWhealy / mindshift03_04.js
Created April 28, 2016 15:48
Mindshift: Part 3, sample 4
// Find the good ones...
var good_kids = kids.filter(k => !k.isNaughty).length;
@ChrisWhealy
ChrisWhealy / mindshift03_05.js
Created April 28, 2016 15:49
Mindshift: Part 3, sample 5
(acc,k) => acc + !k.isNaughty
@ChrisWhealy
ChrisWhealy / mindshift03_06.js
Created April 28, 2016 15:49
Mindshift: Part 3, sample 6
var nice_kids = kids.reduce((acc,k) => acc + !k.isNaughty,0);
@ChrisWhealy
ChrisWhealy / mindshift03_07.js
Created April 28, 2016 15:50
Mindshift: Part 3, sample 7
var naughty_kids = kids.reduce((acc,k) => acc + k.isNaughty,0);
@ChrisWhealy
ChrisWhealy / mindshift03_08.js
Created April 28, 2016 15:51
Mindshift: Part 3, sample 8
var naughty_and_nice = kids.reduce((acc,k) => {
acc.nice += !k.isNaughty;
acc.naughty += k.isNaughty;
return acc;
}, {naughty:0, nice:0});