Skip to content

Instantly share code, notes, and snippets.

View nicholasaiello's full-sized avatar

Nicholas Aiello nicholasaiello

View GitHub Profile
@nicholasaiello
nicholasaiello / scrollTo.js
Created October 3, 2017 19:18
ES6 scrollTo
// 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;
// return the factorial
const fact = (f) => (
Array.from({length: f}, (v, i) => i + 1).reduce((sum, v) => sum *= v, 1)
)
@nicholasaiello
nicholasaiello / memo_fib.js
Created October 3, 2017 21:17
Memoized Fibonacci
const fibo = (() => {
let memo = {};
const _fibo = (n) => {
console.time();
let value;
if (n in memo) {
value = memo[n];
} else {
@nicholasaiello
nicholasaiello / sum_to_n.js
Created October 6, 2017 13:37
Sum a sequence of numbers from 1 ... n
const sumToN = (n) => ( n * (n + 1) / 2 )
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) {
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;
// 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) {
/**
determine character frequency of provided input: ex, "chicago"
print the frequency as follows
c: **
h: *
i: *
a: *
g: *
o: *
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)) {
const powerOfTwo = (num) => (
(num & (num - 1)) === 0
)