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 square = x => x * x; | |
const sum = (x, y) => x + y; | |
const sumOfSquares = (...nums) => nums.reduce((total, num) => sum(total, square(num)), 0); | |
sumOfSquares(1, 2, 3); // 14 |
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 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
const bankAccount = new BankAccount(100); | |
bankAccount.checkBalance(); // 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
const space = (w1, w2) => `${w1} ${w2}`; | |
const sentence = words => `${words.reduce(space)}.`; | |
sentence(['i', 'heart', 'functions']); // => "i heart functions." |
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
/** | |
* With two _objects_, check equality by checking whether they are in fact the same object i.e. the reference. | |
*/ | |
const areAccountsEqual = (a: BankAccount, b: BankAccount) => a === b; | |
const elainesAccount = new BankAccount(100); | |
const georgesAccount = new BankAccount(100); | |
elainesAccount.checkBalance(); // 100 | |
georgesAccount.checkBalance(); // 100 |
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 |