Skip to content

Instantly share code, notes, and snippets.

View FelixLuciano's full-sized avatar
🔭
Discovering

Luciano Felix FelixLuciano

🔭
Discovering
View GitHub Profile
@FelixLuciano
FelixLuciano / My options.js
Last active September 29, 2018 17:19
How many do you have?
new Array(5)
.fill('Option ')
.map((e, i) =>
e + (10 + i).toString(36).toUpperCase()
)
.join('\n')
// But you have no choice
@FelixLuciano
FelixLuciano / getAge.js
Last active February 23, 2022 08:47
Get my age
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
}
@FelixLuciano
FelixLuciano / subscriber.js
Created April 27, 2020 20:42
Simple subscriber
class Subscriber {
constructor () {
this.subscribers = [];
}
subscribe (subscription) {
this.subscribers.push(subscription);
}
notifyAll (...data) {
this.subscribers.forEach(subscriber => subscriber(...data));
}
@FelixLuciano
FelixLuciano / horizontal-scrolling.js
Last active April 10, 2023 11:13
Horizontal scrolling
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()
@FelixLuciano
FelixLuciano / scroll-transform.js
Last active May 27, 2020 16:28
Scroll Transform 💨
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
@FelixLuciano
FelixLuciano / single-div-spinner.js
Last active June 17, 2020 00:11
Single div Spinner SFC
"use strict"
class SingleDivSpinner extends HTMLDivElement {
get value () {
return parseInt(this.style.getPropertyValue('--progress'))
}
set value (newValue) {
const animate = (value) => {
value += (newValue - value) / 4
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
@FelixLuciano
FelixLuciano / calculate-bhaskara.js
Last active July 17, 2020 17:01
Reads a second degree equation in a string and calculates its root using the Bhaskara formula
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)
@FelixLuciano
FelixLuciano / largest-rect.js
Last active January 16, 2021 05:51
Given a n x m binary matrix filled with 0s and 1s, find the largest rectangle containing only 1s and return its area.
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))
}
}
@FelixLuciano
FelixLuciano / can-toggle.js
Created January 16, 2021 05:53
You’re trying to build an IoT mesh network. Signals can only travel the maximum of 5 units. You’re given coordinates for the switch, the light, and the mesh hubs (which capture and forward signals). Return true if the switch can successfully toggle the light.
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)
})
}