Skip to content

Instantly share code, notes, and snippets.

View jonrandy's full-sized avatar
✴️
Sentient

Jon Randy jonrandy

✴️
Sentient
View GitHub Profile
@jonrandy
jonrandy / fizzBuzz.js
Last active February 20, 2023 02:50
FizzBuzz
const fizzBuzz = x=>[x,f='Fizz',b='Buzz',f+b][!(x%5)*2+!(x%3)]
const fizzBuzz = (x,f='Fizz',b='Buzz')=>[x,f,b,f+b][!(x%5)*2+!(x%3)]
const fizzBuzz = x=>({1:x,6:f="Fizz",10:b="Buzz",0:f+b}[x**4%15])
const fizzBuzz = (x,f="Fizz",b="Buzz")=>({1:x,6:f,10:b,0:f+b}[x**4%15])

Every programmer occasionally, when nobody’s home, turns off the lights, pours a glass of scotch, puts on some light German electronica, and opens up a file on their computer. It’s a different file for every programmer. Sometimes they wrote it, sometimes they found it and knew they had to save it. They read over the lines, and weep at their beauty, then the tears turn bitter as they remember the rest of the files and the inevitable collapse of all that is good and true in the world.

This file is Good Code. It has sensible and consistent names for functions and variables. It’s concise. It doesn’t do anything obviously stupid. It has never had to live in the wild, or answer to a sales team. It does exactly one, mundane, specific thing, and it does it well. It was written by a single person, and never touched by another. It reads like poetry written by someone over thirty.

Every programmer starts out writing some perfect little snowflake like this. Then they’re told on Friday they need to have six hundred snow

function fibonacci(n) {
if (n < 0) throw RangeError("Negative arguments not implemented")
return fib(n)[0]
}
function fib(n) {
if (n) {
const [a, b] = fib(~~(n / 2)),
c = a * (b * 2n - a),
d = a * a + b * b
@jonrandy
jonrandy / isPalindrome.js
Created October 31, 2023 08:34
isPalindrome
const isPalindrome = ([...s]) => ''+s == s.reverse()
const fib = n => {
const r5 = fib.r5 || (fib.r5=5**.5)
return Math.floor((((1+r5)/2)**n - ((1-r5)/2)**n)/r5)
}
@jonrandy
jonrandy / flagEmoji.js
Created May 22, 2024 03:39
Get Flag Emoji
// pass in 2 letter country code
const getFlagEmoji = countryCode=>String.fromCodePoint(...[...countryCode.toUpperCase()].map(x=>0x1f1a5+x.charCodeAt()))
@jonrandy
jonrandy / arrayNegativeIndexProxy.js
Created May 31, 2024 11:22
Proxy an Array to allow negative indexes (like Array.at)
const arr = [1, 2, 3, 4]
const handler = {
get(target, prop, receiver) {
// try to convert property to a number
const n = +prop
// if it's a number less than zero, return the correct item from the array
if (!isNaN(n) && n<0) {
return Reflect.get(target, target.length + n, receiver)
// otherwise continue as normal with the property
@jonrandy
jonrandy / observePosition.js
Created June 11, 2024 03:21
Observe Position Changes of an HTML Element
export function observePosition(element, callback) {
const trackDiv = document.createElement("div")
Object.assign(trackDiv.style, {
position: "fixed",
pointerEvents: "none",
width: "2px",
height: "2px",
})
if (element.children.length) {
element.insertBefore(trackDiv, element.firstChild)