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 doNTimesLoop = (n, f) => { | |
for (let i = 1; i <= n; i++) { | |
f(i); | |
} | |
} | |
const doNTimesRec = (n, f) => { | |
const body = (m) => { | |
if (m > n) return; | |
f(m); |
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 range = require("lodash.range") | |
const squaresLodash = (n) => range(1, n + 1).map( | |
(n) => n * n); | |
const squares = (n) => [...Array(n).keys()].map( | |
(n) => (n + 1) * (n + 1)); | |
console.log(squaresLodash(5)); // logs [ 1, 4, 9, 16, 25 ] | |
console.log(squares(5)); // logs [ 1, 4, 9, 16, 25 ] |
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 squaresBad = (n) => { | |
let result = []; | |
for (let i = 1; i <= n; i++) { | |
result.push(i * i); | |
} | |
return result; | |
} | |
const squares = (n) => { | |
let result = new Array(n); |
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 discount = (originalPrices, discountAmount) => { | |
const multiplier = 1 - discountAmount; | |
return originalPrices.map(price => price * multiplier); | |
} | |
const prices = [5, 25, 8, 18]; | |
console.log(discount(prices, 0.2)); // logs [ 4, 20, 6.4, 14.4 ] |
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 discount = (originalPrices, discountAmount) => { | |
const multiplier = 1 - discountAmount; | |
// we must clone the array | |
let result = new Array(originalPrices); | |
for (let i = 0; i < originalPrices.length; i++) { | |
result[i] = originalPrices[i] * multiplier; | |
} | |
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
const sum = (array) => array.reduce( | |
(total, current) => total + current, | |
0); | |
const numbers = [5, 25, 8, 18]; | |
console.log(sum(numbers)); // logs 56 |
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 sum = (array) => { | |
let result = 0; | |
for (let i = 0; i < array.length; i++) { | |
result += array[i]; | |
} | |
return result; | |
} | |
const numbers = [5, 25, 8, 18]; | |
console.log(sum(numbers)); // logs 56 |