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 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!'); |
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
// 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]; | |
}); |
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
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";} |
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
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); |
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 fakePromise(id) { | |
var deferred = $.Deferred(); | |
setTimeout(function() { | |
console.log(id + ' is done'); | |
deferred.resolve(id); | |
}, Math.random() * (1000 - 500) + 500); | |
return deferred.promise(); | |
} | |
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
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)); |
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 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)); | |
}); |
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 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)); | |
}, []); | |
} |
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 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(''); | |
} |
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
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; | |
} |
OlderNewer