Last active
October 10, 2017 01:26
-
-
Save ooade/fe34dc4921ea90f305f0398299ceeb6b to your computer and use it in GitHub Desktop.
CodeWar Challenges
This file contains hidden or 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 translate(sentence) { | |
const vowels = 'aeiou'.split(''); | |
const isUpper = (char => char === char.toUpperCase()); | |
const pigLatin = ((str, i = 0) => { | |
if (vowels.includes(str[0])) { | |
return i === 0 ? str + 'way' : str + 'ay'; | |
} | |
return pigLatin(str.slice(1) + str.slice(0, 1), i + 1); | |
}); | |
return sentence | |
.split(' ') | |
.map(word => { | |
let puncs = word.match(/\W/g); | |
if (puncs) puncs = puncs.join(''); | |
if (puncs) plen = puncs.length; | |
else { | |
plen = 0; | |
puncs = ''; | |
} | |
let latin = pigLatin(word.toLowerCase().slice(0, word.length - plen)) + puncs; | |
if (isUpper(word[0])) latin = latin[0].toUpperCase() + latin.slice(1); | |
return latin; | |
}).join(' '); | |
}; |
This file contains hidden or 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 isValidCoordinates(coord){ | |
let [lat, lon, ...rest] = coord | |
.replace('e','ex') | |
.split(',') | |
.map(Number); | |
if (rest.length > 0) return false; | |
if (lat > 90 || lat < -90) return false; | |
if (lon > 180 || lon < -180) return false; | |
if (isNaN(lat) || isNaN(lon)) { | |
return false; | |
} | |
return true; | |
} |
This file contains hidden or 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 numberToPrice = function(number) { | |
if (!Number.isFinite(number)) { | |
return 'NaN'; | |
} | |
number = number.toLocaleString(); | |
let [num, dec] = number.split('.'); | |
if (dec) { | |
if (dec.length === 1) { | |
dec = dec + '0'; | |
} | |
dec = dec.slice(0, 2); | |
} else { | |
dec = '00'; | |
} | |
return num + '.' + dec; | |
} |
This file contains hidden or 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 listSquared(m, n) { | |
let rootDivisors = []; | |
for (let i = m; i <= n; i++) { | |
let divisors = []; | |
for (let j = 1; j <= i; j++) { | |
// Push every divisor of numbers... | |
if (i % j === 0) divisors.push(j); | |
} | |
let divisorsSquared = divisors.map(x => x * x); | |
let sumOfDivisorsSquared = divisorsSquared.reduce((a, b) => a + b); | |
if (Number.isInteger(Math.sqrt(sumOfDivisorsSquared))) { | |
rootDivisors.push([i, sumOfDivisorsSquared]); | |
} | |
} | |
return rootDivisors; | |
} |
This file contains hidden or 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 Mod4 = /(^(.*)?\[(\+|\-)?(\d+)?(00|04|08|12|16|20|24|28|32|36|40|44|48|52|56|60|64|68|72|76|80|84|88|92|96)\](.*)?$)|(^(.*)?\[(\+|\-)?(0|4|8)\](.*)?$)/ |
This file contains hidden or 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 palindromeChainLength = function(n) { | |
let isPalindrome = x => x.toString().split('').reverse().join('') == x; | |
if (isPalindrome(n)) { | |
return 0; | |
} | |
let steps = 0; | |
while (!isPalindrome(n)) { | |
n = n + Number(n.toString().split('').reverse().join('')); | |
steps++; | |
} | |
return steps; | |
}; |
This file contains hidden or 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 rgb(r, g, b){ | |
/** | |
if x is greater than 0: | |
- if x is greater than 255: | |
x is 255 else 0 | |
else x is 0; | |
**/ | |
const sanitize = x => x > 0 ? (x > 255 ? 255 : x) : 0; | |
r = sanitize(r).toString(16); | |
g = sanitize(g).toString(16); | |
b = sanitize(b).toString(16); | |
const pad = x => (x.length < 2) ? (0 + x) : x; | |
return (pad(r) + pad(g) + pad(b)).toUpperCase(); | |
} |
This file contains hidden or 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 stripUrlParams(url, paramsToStrip = []){ | |
let slug = '', | |
URL = url.split('?')[0], | |
params = url.split(/\?|&/).slice(1); | |
params.reduce((acc, cur, i) => { | |
let [data, value] = cur.split('='); | |
if (!acc[data] && !paramsToStrip.includes(data)) { | |
acc[data] = value; | |
(i === 0) ? slug += `?${data}=${value}` : slug += `&${data}=${value}`; | |
} | |
return acc; | |
}, {}); | |
return URL + slug; | |
} |
This file contains hidden or 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 sumStrings(a, b) { | |
let m = a.split('').reverse(); | |
let n = b.split('').reverse(); | |
let ans = ''; let rem = 0; | |
for (let i = 0; i < Math.max(a.length, b.length); i++) { | |
let s = (m[i] ? +m[i] : 0) + (n[i] ? +n[i] : 0); | |
if (rem !== 0) { | |
s += rem; | |
rem = 0; | |
} | |
if (s < 10) { | |
ans += s; | |
} else { | |
ans += +(s.toString()[1]); | |
rem = +(s.toString()[0]); | |
} | |
} | |
if (rem === 0) { | |
rem = ''; | |
} | |
ans = rem + ans.split('').reverse().join(''); | |
if (ans.length < 20) { | |
ans = Number(ans).toString(); | |
} | |
return ans; | |
} |
This file contains hidden or 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 generateHashtag (str) { | |
let hashedString = '#'; | |
if (str.length === 0 || str.length > 140) return false; | |
for (let word of str.split(' ')) { | |
hashedString += word.charAt(0).toUpperCase() + word.slice(1); | |
} | |
return hashedString; | |
} |
This file contains hidden or 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 validBraces(braces){ | |
do { | |
braces = braces.replace(/\(\)|\[\]|\{\}/, ''); | |
} while (/\(\)|\[\]|\{\}/.test(braces)); | |
return braces.length === 0; | |
} |
This file contains hidden or 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 VigenèreCipher(key, abc) { | |
this.encode = function (str) { | |
return str | |
.split('') | |
.map((c, i) => { | |
return abc.includes(c) ? abc[(abc.indexOf(c) + abc.indexOf(key[i % key.length])) % abc.length] : c | |
}).join(''); | |
}; | |
this.decode = function (str) { | |
return str | |
.split('') | |
.map((c, i) => { | |
let pos = (abc.indexOf(c) - abc.indexOf(key[i % key.length])); | |
return abc.includes(c) ? (pos < 0 ? abc[abc.length - (~pos + 1)] : abc[pos]) : c | |
}).join(''); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment