Skip to content

Instantly share code, notes, and snippets.

View tevko's full-sized avatar
🇺🇲

Tim tevko

🇺🇲
  • NY
View GitHub Profile
const toggle = document.querySelectorAll(".switch input");
toggle.forEach((el) =>
el.addEventListener("click", (e) => {
const isPressed = el.getAttribute("aria-pressed");
el.setAttribute(
"aria-pressed",
// this part is tricky for humans cause "false" === true, since a non empty string is truthy in js
isPressed === "false" ? "true" : "false"
);
// https://www.codewars.com/kata/556206664efbe6376700005c/solutions/javascript/me/best_practice
const isPolydivisible = (n, b) => n.toString().split('').every(
(d, i, a) => (a.join('').slice(0, i + 1))
.split('').reverse()
.map(num => Number(num) || CHARS.indexOf(num))
.reduce((a, c, i) =>
(c * (Math.pow(b, i))) + Number(a)
)
% (i + 1) === 0
function formatDuration (seconds) {
if (seconds === 0) {
return 'now';
}
const dif = seconds * 1000;
const difSeconds = Math.floor(dif / 1000) % 60;
const difMinutes = Math.floor(dif / 60000) % 60;
const difHours = Math.floor(dif / 3.6e+6) % 24;
const difDays = Math.floor(dif / 8.64e+7) % 365;
const difYears = Math.floor(dif / 3.154e+10);
Let's say I have an array of 8 objects. Each object represents a task which can be completed multiple times.
In this case, the goal is to complete each task as many times as possible, until the allotted time per task is up
and I must move on to the next task. Let's also say that I have a total of ten minutes (600 seconds) to spend on
all tasks combined. Using javascript, how can I assign a random number of seconds (no less than 45, assuming each
task takes a minimum of 45 seconds to complete once) to each object so that the combined total of seconds adds up
to exactly 600? In other words, how do I split 600 into 8 random numbers where each random number is >= 45, and
all combined numbers add to 600?
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(BB_SW_CACHE_NAME)
.then(function(cache) {
BB_urlsToCache.map(function(url) {
var request = new Request(url, {mode: 'no-cors'});
fetch(request).then(function(response) {
return caches.open(BB_SW_CACHE_NAME).then(function(c) {
@tevko
tevko / romanNumeralMap.js
Created June 2, 2016 01:28
Roman Numeral Map Javascript
const rm = new Map([
['I', 1],
['II', 2],
['III', 3],
['IV', 4],
['V', 5],
['VI', 6],
['VII', 7],
['VIII', 8],
['IX', 9],
@tevko
tevko / supportsCreateElement.js
Last active February 2, 2016 20:49
Elements on document supporting document.createElement
Array.apply(null, document.querySelectorAll('*')).map(function(e, i, a) {
var message;
try {
document.createElement(e.nodeName);
message = e.nodeName + ' supports document.createElement';
} catch (e) {
message = e.nodeName + ' does not support document.createelement';
}
return message
@tevko
tevko / findParentAttribute.js
Created January 16, 2016 05:49
traverse dom upwards to find an element by it's data attribute
var findParentAttribute = function(id, ctx) {
if (ctx.hasAttribute(id)) {
return ctx.getAttribute(id);
} else {
return this.findParentAttribute(id, ctx.parentElement)
}
return false
}
findParentAttribute('foo', document.querySelector('div').parentElement) // will traverse dom to find parent element with data attribute
@tevko
tevko / bitwise_indexof.js
Created January 15, 2016 17:13
Bitwise Operator and indexOf on strings
// "Bitwise operators perform their operations on such binary representations, but they return standard JavaScript numerical values."
//It transforms -1 into 0, and 0 is false in javascript, so:
var someText = 'text';
!!~someText.indexOf('tex'); //sometext contains text - true
!~someText.indexOf('tex'); //sometext not contains text - false
~someText.indexOf('asd'); //sometext contains asd - false
~someText.indexOf('ext'); //sometext contains ext - true
@tevko
tevko / Custom_Events_Everywhere.js
Created November 10, 2015 18:56
A custom Events utility that works in IE9 and everywhere else
var trigger = function trigger(eventName, elem, data) {
var evt;
data = data || undefined;
try {
evt = new CustomEvent(eventName, {detail: data});
} catch (e) {
evt = document.createEvent('CustomEvent');
evt.initCustomEvent(eventName, true, true, data);
}
elem.dispatchEvent(evt);