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
new Array(5) | |
.fill('Option ') | |
.map((e, i) => | |
e + (10 + i).toString(36).toUpperCase() | |
) | |
.join('\n') | |
// But you have no choice |
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
let me = { | |
birthday: new Date("2000-12-16T19:53:00-02:00"), // YYYY-MM-DDTHH:mm-GMT | |
get age () { | |
const livedTimeInMs = Date.now() - this.birthday.getTime() | |
const ageDate = new Date(livedTimeInMs) | |
const livedYears = ageDate.getUTCFullYear() - 1970 | |
return livedYears | |
} |
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
class Subscriber { | |
constructor () { | |
this.subscribers = []; | |
} | |
subscribe (subscription) { | |
this.subscribers.push(subscription); | |
} | |
notifyAll (...data) { | |
this.subscribers.forEach(subscriber => subscriber(...data)); | |
} |
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
const target = document.querySelector('div') | |
target.addEventListener('wheel', event => { | |
const toLeft = event.deltaY < 0 && target.scrollLeft > 0 | |
const toRight = event.deltaY > 0 && target.scrollLeft < target.scrollWidth - target.clientWidth | |
if (toLeft || toRight) { | |
event.preventDefault() | |
event.stopPropagation() | |
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
const minmax = (value, min = 0, max = 1) => Math.min(Math.max(value, min), max) | |
function scrollTransform (selector, callback) { | |
const element = document.querySelector(selector) | |
const anchor = document.createElement('div') | |
const scroll = { | |
get offsetOut () { | |
value = 1 - (window.scrollY - anchor.offsetTop) / element.offsetHeight | |
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 strict" | |
class SingleDivSpinner extends HTMLDivElement { | |
get value () { | |
return parseInt(this.style.getPropertyValue('--progress')) | |
} | |
set value (newValue) { | |
const animate = (value) => { | |
value += (newValue - value) / 4 |
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 rationalize (value) { | |
let numerator = 0 | |
let denominator = 1 | |
while (value != numerator / denominator) { | |
const ratio = numerator / denominator | |
if (ratio < value) numerator++ | |
else if (ratio > value) denominator++ | |
else break |
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 calculateBhaskara (a, b, c) { | |
if (a === 0) throw(`the coefficient 'a' cannot be 0 in a second degree equation`) | |
const delta = calculateDelta (a, b, c) | |
const solution = [] | |
if (delta < 0) return solution | |
const sqrtDelta = Math.sqrt(delta) | |
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 largestRect(matrix, match = "1") { | |
let maxArea = 0 | |
for (let y1 of matrix.keys()) { | |
for (let x1 of matrix[y1].keys()) { | |
if (matrix[y1][x1] !== match) continue | |
for (var x2 = x1; matrix[y1][x2 + 1] === match; x2++) continue | |
for (var y2 = y1; matrix[y2 + 1]?.slice(x1, x2 + 1).every(a => a === match); y2++) continue | |
maxArea = Math.max(maxArea, (x2 - x1 + 1) * (y2 - y1 + 1)) | |
} | |
} |
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
const distance = ([x1, y1], [x2, y2]) => Math.hypot(x1-x2, y1-y2) | |
function canToggle ({ switch:toggle, hub, light }, radius = 5) { | |
if (distance(toggle, light) <= radius) return true | |
if (!hub.length) return false | |
const steps = hub.filter(step => distance(step, toggle) <= radius) | |
return steps.some(step => { | |
const next = hub.filter(point => !point.every((a, i) => a === step[i])) | |
return canToggle({switch:step, hub:next, light}, radius) | |
}) | |
} |
OlderNewer