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
const factorial = n => { | |
const iterate = (total, i) => { | |
if (i === 0) return total; | |
return iterate(total * i, i - 1); | |
} | |
return iterate(1, n); | |
}; | |
factorial(0); // => 1 | |
factorial(1); // => 1 |
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
let bankAccount = 100; | |
bankAccount = bankAccount - 20; | |
bankAccount; // 80 |
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
const makeBankAccount = balance => ({ | |
withdraw: amount => balance = balance - amount, // <-- assign `balance` a new value | |
checkBalance: () => balance | |
}); | |
const bankAccount = makeBankAccount(100); | |
bankAccount.withdraw(20); | |
bankAccount.checkBalance(); // 80 |
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
class BankAccount { | |
private balance; | |
constructor(funds) { | |
this.balance = funds; | |
} | |
public withdraw(amount) { | |
this.balance = this.balance - amount; // <-- assign `this.balance` a new value | |
} |
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
const factorial = n => { | |
if (n === 0) return 1; | |
return n * factorial(n - 1); | |
}; | |
factorial(1); // => 1 | |
factorial(2); // => 2 | |
factorial(3); // => 6 |
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
const factorial = n => { | |
let total = 1; | |
while (n > 0) { | |
total = total * n; | |
n = n - 1; | |
} | |
return total; | |
}; | |
factorial(0); // => 1 |
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
const decrement100 = x => { | |
let r = 100; | |
r = 100 - x; | |
return r; | |
}; | |
decrement100(20); // => 80 |
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
const buildCallbacks = items => { | |
const callbacks = []; | |
let i; | |
for (i = 0; i < items.length; i++) { | |
callbacks.push(() => items[i]); | |
} | |
return callbacks; | |
} | |
const callbacks = buildCallbacks(["hello", "cruel", "world"]); |
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
const add10 = n => { | |
let sum = n; | |
for (let i = 0; i < 10; i++) { | |
sum = sum + 1; | |
} | |
return sum; | |
}; | |
add10(2); // => 12 | |
add10(1); // => 11 |
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
const space = (w1, w2) => `${w1} ${w2}`; | |
const sentence = words => `${words.reduce(space)}.`; | |
sentence(['i', 'heart', 'functions']); // => "i heart functions." |