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 getType(obj) { | |
// slice first 8 characters of, e.g., "[object String]" or "[object Object]" to get type | |
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase(); | |
} | |
getType("foo"); // "string" | |
getType([]); // "array" | |
getType({}); // "object" | |
getType(new Set()); // "set" | |
getType(true); // "boolean" |
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
// https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 | |
// | |
// Requires lodash or underscore for the `range` function | |
function calculatePi(nTerms) { | |
var numerator = 4; | |
var denominator = 1; | |
var operation = 1; | |
var pi = 0; | |
const range = _.range(nTerms); |
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
// Generates Pascal's triangle of any number of rows | |
// | |
// https://en.wikipedia.org/wiki/Pascal%27s_triangle | |
function nextRow(row) { | |
const right = row.concat(0) | |
const left = [0].concat(row) | |
return left.map((x, i) => x + right[i]); | |
} |
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
body { | |
font-family: 'Helvetica'; | |
display: flex; | |
flex-direction: column; | |
align-items: center; | |
} | |
div.container { | |
width: 1050px; | |
min-width: 1050px; |
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
<!-- based on the pioneering graphic design of Josef Müller-Brockmann --> | |
<!-- see the basis for this here: | |
https://i1.wp.com/www.grapheine.com/wp-content/uploads/2013/03/MULLER-BROCKMANN-music-geometric-poster.jpg?quality=90&strip=all&ssl=1 | |
--> | |
<!-- learn more about Josef Müller-Brockmann here: https://www.grapheine.com/en/history-of-graphic-design/graphic-designer-muller-brockmann-swiss-style --> | |
<div class="poster-container"> | |
<div class="poster"> | |
<svg version="1.1" xmlns="http://www.w3.org/2000/svg"> |
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
// Remove duplicate words in a string | |
// Uses ES5 | |
function removeDuplicates(str) { | |
const split = str.split(' '); | |
const newArray = split.reduce((accumulator, currentValue) => { | |
if (!accumulator.includes(currentValue)) { | |
accumulator.push(currentValue); | |
} |
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
// Write a higher-order function loop that provides something like a for loop statement. | |
// It takes a value, a test function, an update function, and a body function. Each | |
// iteration, it first runs the test function on the current loop value and stops if | |
// that returns false. Then it calls the body function, giving it the current value. | |
// Finally, it calls the update function to create a new value and starts from the beginning. | |
// When defining the function, you can use a regular loop to do the actual looping. | |
const loop = (value, testFunc, updateFunc, bodyFunc) => { | |
if (testFunc(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
// Use the reduce method in combination with the concat method to | |
// “flatten” an array of arrays into a single array that has all | |
// the elements of the original arrays. | |
// My original "solution": | |
let arrays = [[1, 2, 3], [4, 5], [6]]; | |
arrays.reduce((accumulator, currentValue) => accumulator.concat(currentValue),[]); // [1, 2, 3, 4, 5, 6] | |
// This solution works on the sole test case in Chapter 5 of Eloquent JavaScript; but it doesn't work with nested arrays. |
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
// Write a function min that takes two arguments and returns their minimum. | |
const minimum = (num1, num2) => num1 > num2 ? num2 : num1 | |
minimum(1,300) // 1 | |
minimum(-1,300) // -1 | |
minimum(-1,-300) // -300 |
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
// We’ve seen that % (the remainder operator) can be used to test whether | |
// a number is even or odd by using % 2 to see whether it’s divisible by two. | |
// Here’s another way to define whether a positive whole number is even or odd: | |
// | |
// Zero is even. | |
// | |
// One is odd. | |
// | |
// For any other number N, its evenness is the same as N - 2. |
NewerOlder