Skip to content

Instantly share code, notes, and snippets.

View layonthebeech's full-sized avatar

Pat Beecher layonthebeech

  • Chicago, IL
View GitHub Profile
var str = "aaaaaaaa";
var word = "aaa";
var ltr = "w";
var result = "";
for(var i = 0; i < str.length; i++) {
//console.log(str)
@layonthebeech
layonthebeech / ransomnote.js
Created September 29, 2016 20:18
Ransom note question on hackerrank solution
function main() {
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
magazine = readLine().split(' ');
ransom = readLine().split(' ');
var arr = []
var dict = {}
for(var i = 0; i < magazine.length; i++) {
if(dict[magazine[i]]) {
@layonthebeech
layonthebeech / primefactorstesteroony.js
Created September 27, 2016 14:13
Description: You have to code a function getAllPrimeFactors which take an integer as parameter and return an array containing its prime decomposition by ascending factors, if a factors appears multiple time in the decomposition it should appear as many time in the array. exemple: getAllPrimeFactors(100) returns [2,2,5,5] in this order. This deco…
function sieve(x) {
var arr = [];
var output = [];
for(var i = 0; i < x; i++) {
arr.push(true);
}
for(var i = 2; i <= Math.sqrt(x); i++) {
if(arr[i]) {
for(var j = i*i; j <x; j+=i) {
arr[j] = false;
@layonthebeech
layonthebeech / returnfunctionextravaganza.js
Created September 23, 2016 22:22
Interesting Kata where you are calling functions like so five(times(five()) and you want it to execute to equal 25
var n = function(digit) {
return function(op) {
return op ? op(digit) : digit;
}
};
var zero = n(0);
var one = n(1);
var two = n(2);
var three = n(3);
var four = n(4);
@layonthebeech
layonthebeech / addfunctofuncprototype.js
Last active September 22, 2016 19:02
Add a function "pipe" to the function prototype
Function.prototype.pipe = function(fun) {
return function(param) {
return fun(this(param));
}.bind(this); //The bind() method creates a new function that, when called, has its this keyword
}; //set to the provided value, with a given sequence of arguments preceding any provided
//when the new function is called.
@layonthebeech
layonthebeech / returnfunctiononce.js
Created September 3, 2016 15:27
Return a function that can only be called once
function once(fn) {
var call = true
return function() {
if (call) {
call = false
return fn.apply(this, arguments)
}
}
}
@layonthebeech
layonthebeech / compose.js
Created September 1, 2016 23:21
two function compose
function compose(f, g) {
return function() {
return f(g.apply(this, arguments));
};
}
@layonthebeech
layonthebeech / iqevenoddtest.js
Created September 1, 2016 16:48
IQ even odd test
function iqTest(numbers){
numbers = numbers.split(" ");
var even = numbers.filter(function(number) {
return number % 2 === 0;
});
var odd = numbers.filter(function(number) {
return number % 2 !== 0;
});
console.log(even, odd);
if(even.length > odd.length) return numbers.indexOf(odd[0])+1;
Array.prototype.int = function (){
return this.filter(function (x) { return typeof x == 'number' && x == ~~x });
}
Array.prototype.even = function(){
return this.int().filter(function (x) { return ~x & 1 });
}
Array.prototype.odd = function(){
return this.int().filter(function (x) { return x & 1 });
}