Last active
June 24, 2020 19:22
-
-
Save justrdk/4c276bf241ce89ede0df to your computer and use it in GitHub Desktop.
some of my solutions for some katas on codewars
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
//default configuration | |
function defaults(obj, defs) { | |
result = {}; | |
for (var key in obj) { | |
result[key] = obj[key]; | |
} | |
for (var key in defs) { | |
if (!obj.hasOwnProperty(key)) { | |
result[key] = defs[key]; | |
if (result[key].error !== undefined) { | |
throw result[key].error | |
} | |
} | |
} | |
return result; | |
} | |
function mandatory(err) { | |
return { | |
error: err | |
}; | |
} | |
//Let's make a function called compose that accepts a value as a parameter, as well as any number of functions as additional parameters.The function will return the value that results from the first parameter being used as a parameter for all of the accepted function parameters in turn. | |
var compose = function() { | |
var value = arguments[0]; | |
var result; | |
var args = [].slice.call(arguments); | |
if (arguments.length === 1) { | |
return arguments[0]; | |
} | |
args.forEach(function(arg, index) { | |
if (typeof arg === "function") { | |
if (index === 1) { | |
result = arg.call(this, value); | |
} else { | |
result = arg.call(this, result); | |
} | |
} | |
}); | |
return result; | |
}; | |
//Write a function evilTwin(obj) which returns a new object with all the same properties as obj, and with an additional property hasGoatee set to true. | |
function evilTwin(orig) { | |
function evilBro() {}; | |
evilBro.prototype = orig; | |
var twin = new evilBro(); | |
twin.constructor = evilBro; | |
twin.hasGoatee = true; | |
return twin; | |
} | |
//dependency injection function | |
var DI = function(dependency) { | |
this.dependency = dependency; | |
}; | |
DI.prototype.inject = function(func) { | |
var funcString = func.toString(); | |
var matches = funcString.match(/^function\s+?.*?\((.*?)\)/); | |
var argsNames = matches[1].split(',').map(function(arg) { | |
return arg.trim(); | |
}); | |
var dependency = this.dependency; | |
return function() { | |
var args = argsNames.reduce(function(args, argName) { | |
argName && args.push(dependency[argName]); | |
return args; | |
}, []); | |
return func.apply(this, args); | |
}; | |
} | |
//Create a combinator function named flip that takes a function as an argument and returns that function with it's arguments reversed. | |
function flip(fn) { | |
return function() { | |
return fn.apply(this, Array.prototype.slice.call(arguments).reverse()) | |
} | |
} | |
//Roman Numerals Encoder | |
function solution(number) { | |
var romanDecimals = { | |
M: 1000, | |
CM: 900, | |
D: 500, | |
CD: 400, | |
C: 100, | |
XC: 90, | |
L: 50, | |
XL: 40, | |
X: 10, | |
IX: 9, | |
V: 5, | |
IV: 4, | |
I: 1 | |
}; | |
var romanNumber = ''; | |
for (var roman in romanDecimals) { | |
while (number >= romanDecimals[roman]) { | |
romanNumber += roman; | |
number -= romanDecimals[roman]; | |
} | |
} | |
return romanNumber; | |
} | |
//Largest 5 digit number in a series | |
function solution(digits) { | |
var arr = digits.split(""); | |
arr.forEach(function(digit, index) { | |
arr[index] = parseInt(arr.slice(index, index + 5).join(""), 10); | |
}); | |
return Math.max.apply(Math, arr.filter(function(digit) { | |
return digit.toString().length === 5; | |
})); | |
} | |
//Your goal in this kata is to implement an difference function, which subtracts one list from another. | |
//It should remove all values from list a, which are present in list b. | |
function array_diff(a, b) { | |
var result = []; | |
var filterNonRejects = function(value) { | |
return function(element) { | |
return element !== value; | |
} | |
}; | |
if (b.length === 0) { | |
return a; | |
} | |
b.forEach(function(rejectItem) { | |
result.push(a.filter(filterNonRejects(rejectItem))); | |
}); | |
return [].concat.apply([], result); | |
} | |
//The numberOfOccurrences function must return the number of occurrences of an element in an array | |
Array.prototype.numberOfOccurrences = function(nr) { | |
return this.filter(function(element) { | |
return element === nr; | |
}).length; | |
} | |
//RGB to hex conversion | |
function rgb(r, g, b) { | |
function toHex(rgb) { | |
if (rgb < 0) { | |
return "00"; | |
} | |
if (rgb > 255) { | |
return "FF"; | |
} | |
var hex = rgb.toString(16); | |
return hex.length == 1 ? "0" + hex.toUpperCase() : hex.toUpperCase(); | |
} | |
return toHex(r) + toHex(g) + toHex(b); | |
} | |
//Given two arrays, the purpose of this Kata is to check if these two arrays are the same. "The same" in this Kata means the two arrays contains arrays of 2 numbers which are same and not necessarily sorted the same way. i.e. [[2,5], [3,6]] is same as [[5,2], [3,6]] or [[6,3], [5,2]] or [[6,3], [2,5]] | |
function same(aArr, bArr) { | |
if (aArr.length === 0 && bArr.length === 0) { | |
return true; | |
} | |
if (aArr.length !== bArr.length) { | |
return false; | |
} | |
aArr = aArr.map(function(element) { | |
return element.sort(); | |
}); | |
bArr = bArr.map(function(element) { | |
return element.sort(); | |
}); | |
var result = []; | |
var flag = 0; | |
aArr.forEach(function(aItem) { | |
var temp = bArr.filter(function(bItem) { | |
return bItem[0] - aItem[0] === 0 && bItem[1] - aItem[1] === 0; | |
})[0]; | |
if (temp === undefined) { | |
flag++; | |
} | |
}); | |
return flag === 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment