Skip to content

Instantly share code, notes, and snippets.

View SeeThruHead's full-sized avatar

Shane Keulen SeeThruHead

View GitHub Profile
@SeeThruHead
SeeThruHead / checkInputSum
Created December 8, 2014 05:42
Throw error if a group of inputs is over a certain sum
function checkSum(groupOfInputs, maxValue) {
groupOfInputs = $(groupOfInputs);
groupOfInputs.on('change keyup paste', function() {
$this = $(this);
var values = groupOfInputs.map(function() {return $(this).val();}).get();
var sum = values.reduce(function(one, two) {
return Number(one) + Number(two);
});
if (sum > maxValue) {
alert('Too big!');
@SeeThruHead
SeeThruHead / fillArray.js
Created December 20, 2014 20:44
Fill an array with another array, repeating when necessary.
// Init a source array contains numbers 0-35
var source = Array.apply(null, {length: 36}).map(Number.call, Number);
// Init the result array as undefined using the arguments pseudo array.
var result = Array.apply(null, {length: 100});
// The magic
result = result.map(function(val, index) {
return source[index % source.length];
});
console.log(
Array.apply(null, {length: 100}).map(function(val, index) {
return (++index%3?'':'Fizz')+(index%5?'':'Buzz')||index;
}).join('\n')
);
console.log(
Array.apply(null, {length: 100}).map(function(val, index) {
index++;
if (index % 15 == 0){return "FizzBuzz";}
@SeeThruHead
SeeThruHead / balanced-parens.js
Last active August 29, 2015 14:23
Balanced Parens
module.exports = function checkBal(input) {
function match(left, right) {
return left === '(' && right === ')';
}
function removeLast(ar) {
return ar.slice(0, ar.length - 1);
}
var result = input.split('').reduce(function(prev, curr, index, array) {
if (!prev.length) return [curr];
if (match(prev[prev.length -1], curr)) return removeLast(prev);
function fakePromise(id) {
var deferred = $.Deferred();
setTimeout(function() {
console.log(id + ' is done');
deferred.resolve(id);
}, Math.random() * (1000 - 500) + 500);
return deferred.promise();
}
String.prototype.repeatify = function(x) {
// Array.apply(null, {length: x}) creates an array of undefined values with length x
return Array.apply(null, {length: x}).map(() => this).join('');
};
// or a better but less fun solution
String.prototype.repeatify = function(x) {
return Array(x + 1).join(this);
};
console.log('hi'.repeatify(10));
function biggestGap(values) {
function diff(curr, before) {
return curr - Math.min.apply(null, before);
}
function diffs(values) {
return values.map(function(x, i, ar) {
return diff(x, ar.slice(0, i));
});
function isInt(x) {
return (typeof x === 'number') && (x % 1 === 0);
}
function flatten(ar) {
return ar.reduce(function(prev, curr) {
return prev.concat((Array.isArray(curr) ? flatten(curr) : curr));
}, []);
}
@SeeThruHead
SeeThruHead / duplicateEncoder.js
Created October 19, 2015 04:28
Check for duplicate chars, if duplicated replace with ')' else '('
const check = (char, ind, word) => ~word.slice(0, ind).indexOf(char)
|| ~word.slice(ind + 1).indexOf(char) ? ')' : '(';
function duplicateEncode(word) {
return word
.toLowerCase()
.split('')
.map(check)
.join('');
}
var durations = ["12:38", "6:36", "9:03", "8:34", "5:02", "6:54", "13:22", "4:41", "8:36", "21:58", "3:06", "10:46", "10:13", "12:54", "14:00", "11:03", "16:03", "10:52", "24:53", "10:03", "11:49", "15:47", "3:19", "2:06", "5:47", "1:03", "5:29", "5:47", "26:39"];
// write a function that will take the above array of string durations and convert it hours/mins/seconds
// You can use any JS you want - loops/map/reduce/etc...
function addDurations(acc, curr) {
const [ seconds = 0, minutes = 0, hours = 0] = curr.split(':')
.reverse().map((x) => parseInt(x, 10));
return acc += hours * 3600 + minutes * 60 + seconds;
}