Skip to content

Instantly share code, notes, and snippets.

View kdubbels's full-sized avatar
🔮
doing orb stuff

Kristofer Dubbels kdubbels

🔮
doing orb stuff
View GitHub Profile
@kdubbels
kdubbels / getType.js
Last active June 3, 2021 17:49
Get the type of a value from its prototype
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"
@kdubbels
kdubbels / calculatePi.js
Created January 16, 2021 05:14
Calculate digits of pi using Leibniz's formula
// 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);
@kdubbels
kdubbels / pascal.js
Last active January 26, 2021 21:19
Pascal's Triangle
// 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]);
}
@kdubbels
kdubbels / beethoven.css
Last active December 3, 2020 22:36
Beethoven by Josef Müller-Brockmann
body {
font-family: 'Helvetica';
display: flex;
flex-direction: column;
align-items: center;
}
div.container {
width: 1050px;
min-width: 1050px;
@kdubbels
kdubbels / musica-viva.html
Created November 29, 2020 06:27
Musica Viva by Josef Müller-Brockmann
<!-- 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">
@kdubbels
kdubbels / removeDuplicateWordsInAString.js
Last active August 19, 2020 17:26
Remove duplicate words in a string
// 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);
}
@kdubbels
kdubbels / yourOwnLoop.js
Created August 19, 2020 00:44
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 call…
// 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)) {
@kdubbels
kdubbels / flatten.js
Last active August 19, 2020 18:16
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.
// 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.
@kdubbels
kdubbels / minimum.js
Created August 18, 2020 17:19
Write a function min that takes two arguments and returns their minimum.
// 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
@kdubbels
kdubbels / isEven.js
Created August 18, 2020 17:02
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. Define a recursive function isEven corr…
// 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.