Skip to content

Instantly share code, notes, and snippets.

View jonrandy's full-sized avatar
✴️
Sentient

Jon Randy jonrandy

✴️
Sentient
View GitHub Profile
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 / isPalindrome.js
Created October 31, 2023 08:34
isPalindrome
const isPalindrome = ([...s]) => ''+s == s.reverse()
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

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

@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])
@jonrandy
jonrandy / isPrimeRegex.js
Last active May 31, 2022 03:34
isPrime using regex
const isPrime = x=>!'1'.repeat(x).match(/^1?$|^(11+?)\1+$/)
@jonrandy
jonrandy / isValidCSSColour.js
Created January 5, 2022 04:09
isValidCSSColour
const isValidCSSColour = str => {
const s = new Option().style
s.color = str
return s.color != ''
}
@jonrandy
jonrandy / numberSpread.js
Last active September 13, 2021 05:28
Spreading Numbers
Number.prototype[Symbol.iterator] = function* () {
for (let i = 1; i <= this; i++) yield i
}
[...5] // [1,2,3,4,5]
@jonrandy
jonrandy / getFlagEmoji.js
Created April 9, 2021 11:26
Two letter country code to Flag Emoji
const getFlagEmoji = countryCode=>String.fromCodePoint(...[...countryCode.toUpperCase()].map(x=>0x1f1a5+x.charCodeAt(0)))