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
// Linear scrollTo | |
// used more than once, so better off in function | |
const getNowTimestamp = () => ( | |
'now' in window.performance ? performance.now() : +new Date | |
); | |
const scrollTo = (dest, duration = 300, callback) => { | |
const body = document.body, | |
docElement = document.documentElement; |
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
// return the factorial | |
const fact = (f) => ( | |
Array.from({length: f}, (v, i) => i + 1).reduce((sum, v) => sum *= v, 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 fibo = (() => { | |
let memo = {}; | |
const _fibo = (n) => { | |
console.time(); | |
let value; | |
if (n in memo) { | |
value = memo[n]; | |
} else { |
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 sumToN = (n) => ( n * (n + 1) / 2 ) |
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 checkBalanced(str, openChar = '(', closeChar = ')') { | |
// validate input | |
if (!str || !str.trim().length) { | |
return false; | |
} | |
const matches = str.match(new RegExp(`(\\${openChar}|\\${closeChar})`, 'g')); | |
// check for odd # | |
if (matches.length % 2 !== 0) { |
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 input = "who tried never Made anything a A Mistake never New Person.", | |
stripPunctuation = (str) => str.replace(/[\.\,\?\-\_\!]/, ''), | |
isLowerCase = (str) => !(str.charCodeAt(0) >= 65 && str.charCodeAt(0) <= 90), | |
format = (arr) => arr.reverse().join(' ') | |
const sortFn = (a, b) => { | |
if (isLowerCase(a) && isLowerCase(b)) { | |
return a.localeCompare(b) * -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
// Given a string "aaabbbccdeff", compress it, = "a3b3c2def2" | |
const input = "aaabbbccdeff"; | |
function compressWord(str = '') { | |
const size = (str || '').length; | |
// edge cases | |
if (size < 2) { | |
return str; | |
} else if (size === 2) { |
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
/** | |
determine character frequency of provided input: ex, "chicago" | |
print the frequency as follows | |
c: ** | |
h: * | |
i: * | |
a: * | |
g: * | |
o: * |
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 words = ['map', 'art', 'how', 'rat', 'tar', 'who', 'pam', 'shoop']; | |
const sortWord = (word) => ( | |
word.split('').sort().join('') | |
); | |
const matches = words.reduce((obj,w) => { | |
if (w) { | |
const k = sortWord(w); | |
if (!(k in obj)) { |
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 powerOfTwo = (num) => ( | |
(num & (num - 1)) === 0 | |
) |
OlderNewer