Skip to content

Instantly share code, notes, and snippets.

@abhisekp
Last active November 23, 2016 07:51
Show Gist options
  • Save abhisekp/a891a7318160c989f251 to your computer and use it in GitHub Desktop.
Save abhisekp/a891a7318160c989f251 to your computer and use it in GitHub Desktop.
One Liner Solutions to FreeCodeCamp Challenges - http://j.mp/abhisekpFCCSolnsOneLiners
function binaryAgent(str) {
return str.replace(/\d+\s?/g, val => String.fromCodePoint(parseInt(val, 2)))
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")
function binaryAgent(str) {
return String.fromCodePoint.apply(
null,
str.split(' ').map(val => parseInt(val, 2))
)
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")
function boo(bool) {
// What is the new fad diet for ghost developers? The Boolean.
// return typeof bool === 'boolean';
return !!bool === bool;
}
boo(null);
function rot13(str) { // LBH QVQ VG!
return str.replace(/[A-Z]/g, (L) => String.fromCharCode(65 + L.charCodeAt(0) % 26));
}
function palindrome(str) {
return (str = str.toLowerCase().replace(/\W|_/g, '')) === str.split('').reverse().join('');
}
palindrome("0_0 (: /-\\ :) 0-0");
function end(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
// "I find my own luck!" -- AbhisekP.
return new RegExp(target + '$').test(str);
}
function end(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
// "I find my own luck!" -- AbhisekP.
return target === str.substr(str.length - target.length);
}
function end(str, target) {
// "Never give up and good luck will find you."
// -- Falcor
// "I find my own luck!" -- AbhisekP.
return str.lastIndexOf(target) === str.length - target.length;
}
function diffArray(arr1, arr2) {
// Same, same; but different.
return arr1.filter(el => !arr2.includes(el))
.concat(arr2.filter(el => !arr1.includes(el)));
}
diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
function pair(str) {
return str.split('').map((dna) => [dna, 'ATGC'['TACG'.indexOf(dna)]]);
}
pair("ATCGA");
function every(collection, pre) {
// Does everyone have one of these?
// Do they have to?
return collection.every((obj) => obj[pre]);
}
every([{'user': 'Tinky-Winky', 'sex': 'male'}, {'user': 'Dipsy', 'sex': 'male'}, {'user': 'Laa-Laa', 'sex': 'female'}, {'user': 'Po', 'sex': 'female'}], 'sex');
function factorialize(num) {
return (num < 2) ? 1 : (num * factorialize(num - 1));
}
function bouncer(arr) {
// Don't show a false ID to this bouncer.
// I've a hammer fist.
return arr.filter(Boolean);
}
bouncer([7, 'ate', '', false, 9]);
function findLongestWord(str) {
return Math.max(...str.split(' ').map(word => word.length));
}
findLongestWord("The quick brown fox jumped over the lazy dog");
function find(arr, func) {
// Find me if you can
return arr.filter(func)[0];
}
find([1, 2, 3, 4], function(num){ return num % 2 === 0; });
function mutation(arr) {
// change is the only constant in universe
return arr[1].toLowerCase().split('').every(letter => arr[0].toLowerCase().includes(letter));
}
function translatePigLatin(str) {
return /^[aeiou]/.test(str)
? str + 'way'
: str.replace(/([^aeiou]+)(.+)/, '$2$1ay')
}
translatePigLatin("consonant")
function repeat(str, num) {
// repeat after me
// am i in class 2 or something?
return num <= 0 ? '': str + repeat(str, num-1);
}
repeat('abc', 3);
function largestOfFour(arr) {
// You can do this!
// Yes, I can. :P
return arr.map(e => Math.max(...e));
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
function reverseString(str) {
return str.split("").reverse().join("");
}
reverseString('hello');
function destroyer(arr, ...args) {
return arr.filter((el) => !args.includes(el))
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3)
function slasher(arr, howMany) {
// it doesn't always pay to be first
// well, now my head is chopped off >:(
return arr.slice(howMany);
}
slasher([1, 2, 3], 2);
function uniteUnique(...arrs) {
return [].concat(...arrs).filter(
(val, idx, currArr) => currArr.indexOf(val) === idx
)
}
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str.split(/\s|_|(?=[A-Z])/).join('-').toLowerCase()
}
spinalCase('This Is Spinal Tap')
function steamrollArray (arr) {
return Array.isArray(arr)
? [].concat.apply([], arr.map(steamrollArray))
: arr
}
steamrollArray([1, [2], [3, [[4]]]])
function sym(...arrays) {
// Same, Same
// But different
return arrays.reduce((symDiff, arr) =>
symDiff.concat(arr)
.filter((val, idx, theArr) =>
theArr.indexOf(val) === idx && !(symDiff.includes(val) && arr.includes(val))
)
)
}
sym([1, 2, 3], [5, 2, 1, 4]);
function titleCase(str) {
return str.toLowerCase().replace(/(?: |^)[a-z]/g, (L) => L.toUpperCase());
}
titleCase("I'm a little tea pot");
function truncate(str, num) {
// Clear out that junk in your trunk
// trunk is loaded
return str.length > num ? num <= 3 ? str.substr(0, num) + '...': str.substr(0, num-3) + '...' : str;
}
function telephoneCheck(str) {
// Good luck!
// I'm feeling lucky today!
return /^(1)?(((?:\s*\(\s*)(\d{3})(?:\s*\)\s*))|((\s*|-)\d{3}(\s*|-)))(\d{3})(\s*-?\s*)(\d{4})$/.test(str);
}
telephoneCheck("555-555-5555");
function where(arr, num) {
// Find my place in this sorted array.
// Why? Are you lost?
return arr.reduce((pos, curr) => num <= curr ? pos : pos + 1, 0);
}
where([40, 60], 50);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment