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 / ackermann.js
Created August 17, 2017 00:07
Ackermann function in ES6
const ackermann = (m, n) => {
if (m === 0) {
return n+1
}
if (n === 0) {
return ackermann((m - 1), 1);
}
if (m !== 0 && n !== 0) {
@kdubbels
kdubbels / README.md
Last active December 5, 2021 07:06
GitHub Languages Breakdown Pie Chart with React + D3

Enter GitHub username and your GitHub API token to see a breakdown of all your total repos by language.

Color scheme provided by GitHub.

Apologies for loading time- this uses React, React-DOM, Babel, jQuery, and D3.

Peace and love.

@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 / 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 / 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 / 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 / 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 / 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)) {