This file contains hidden or 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
<> |
This file contains hidden or 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 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" | |
); |
This file contains hidden or 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
// 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 |
This file contains hidden or 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 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); |
This file contains hidden or 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'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? |
This file contains hidden or 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
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) { |
This file contains hidden or 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 rm = new Map([ | |
['I', 1], | |
['II', 2], | |
['III', 3], | |
['IV', 4], | |
['V', 5], | |
['VI', 6], | |
['VII', 7], | |
['VIII', 8], | |
['IX', 9], |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
// "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 |
NewerOlder