Last active
April 6, 2017 10:46
-
-
Save Masd925/8b1a9b1d37c322827dcd to your computer and use it in GitHub Desktop.
Some one-liner solutions on FCC exercises. Most are my own doing. Some picked from FCC chat.
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 add() { | |
| return (arguments.length===2 && typeof(arguments[0])==='number' && typeof(arguments[1])==='number') ? | |
| arguments[0] + arguments[1] : | |
| (arguments.length===1 && typeof(arguments[0])==='number') ? add.bind(null, arguments[0]) : undefined; | |
| } |
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
| Some one-liner solutions on FCC exercises. Most are my own doing. Some picked from FCC chat. | |
| ----Arguments optional---- | |
| function add() { | |
| return (arguments.length===2 && typeof(arguments[0])==='number' && typeof(arguments[1])==='number') ? | |
| arguments[0] + arguments[1] : | |
| (arguments.length===1 && typeof(arguments[0])==='number') ? add.bind(null, arguments[0]) : undefined; | |
| } | |
| ----Binary Agents---- | |
| function binaryAgent(str) { | |
| return str.split(" ").map(function(elem){ | |
| return String.fromCharCode(parseInt(elem,2)); | |
| }).join(""); | |
| } | |
| ----Boo who---- | |
| function boo(bool) { | |
| return !!bool===bool; | |
| } | |
| ----Caesars Cipher---- | |
| function rot13(str) { | |
| return str.split("").map(function(char){ | |
| return (char.charCodeAt(0)>64 && char.charCodeAt(0)<91) ? String.fromCharCode(char.charCodeAt(0)%26+65) : char; | |
| }).join(""); | |
| } | |
| ----Check for palindromes---- | |
| function palindrome(str) { | |
| return str.replace(/[\W_]/g,"").toLowerCase().split("").every(function(elem,index,array){ | |
| return elem===array[array.length-1-index]; | |
| }); | |
| } | |
| ----Chunky monkey---- | |
| function chunk(arr, size) { | |
| return arr.length ? [arr.splice(0,size)].concat(chunk(arr,size)):[]; | |
| } | |
| ----Confirm the ending---- | |
| function end(str, target) { | |
| return str.slice(-target.length)===target; | |
| } | |
| ----Counting cards---- | |
| function cc(card) { | |
| return (count+={2:1,3:1,4:1,5:1,6:1,7:0,8:0,9:0,10:-1,J:-1,Q:-1,K:-1,A:-1}[card]) + " " + (count>0 ? "Bet" : "Hold"); | |
| } | |
| ----Diff two arrays---- | |
| function diff(arr1, arr2) { | |
| return arr1.concat(arr2).filter(function(val, index, array){ | |
| return array.indexOf(val)===array.lastIndexOf(val); | |
| }); | |
| } | |
| ----DNA pairing---- | |
| function pair(str){ | |
| return Array.prototype.map.call(str, function(val){ | |
| return [val,{A:'T', T: 'A', C: 'G', G: 'C'}[val]]; | |
| }); | |
| } | |
| ----Drop it---- | |
| function drop(arr, func) { | |
| return (arr.length && !func(arr[0])) ? drop(arr.slice(1),func) : arr; | |
| } | |
| ----Everything be true---- | |
| function every(collection, pre) { | |
| return collection.every(function(obj){ | |
| return obj[pre]; | |
| }); | |
| } | |
| ----Falsy bouncer---- | |
| function bouncer(arr) { | |
| return arr.filter(function(val){return val;}); | |
| } | |
| ----Finders keepers---- | |
| function find(arr, func) { | |
| return arr.length ? (func(arr[0]) ? arr[0] : find(arr.slice(1),func)) : undefined; | |
| } | |
| ----Find the longest word in a string---- | |
| function findLongestWord(str) { | |
| return str.split(' ').reduce(function(prev, curr) { | |
| return Math.max(prev, curr.length); | |
| }, 0); | |
| } | |
| ----Missing letters---- | |
| function fearNotLetter(str) { | |
| if (str.length>1) return str.charCodeAt(1)===str.charCodeAt(0)+1 ? fearNotLetter(str.slice(1)) : String.fromCharCode(str.charCodeAt(0)+1); | |
| } | |
| ----Mutations---- | |
| function mutation(arr) { | |
| return arr[1].toLowerCase().split("").every(function(val, index, array){ | |
| return arr[0].toLowerCase().indexOf(val)>-1; | |
| }); | |
| } | |
| ----Pairwise---- | |
| function pairwise(arr, arg) { | |
| return arr.reduce(function(prev,curr,index){ | |
| arr.forEach(function(elem,ind){ | |
| if (ind>index && curr+elem===arg && prev.indexOf(index)===-1 && prev.indexOf(ind)===-1) prev.push(index,ind); | |
| }); | |
| return prev; | |
| },[]).reduce(function(cur,pre){return cur+pre;},0); | |
| } | |
| ----Repeat a string---- | |
| function repeat(str, num) { | |
| return num>0 ? str+repeat(str,num-1) : ""; | |
| } | |
| ----Return largest numbers in arrays---- | |
| function largestOfFour(arr) { | |
| return arr.map(function(innerArray){ | |
| return Math.max.apply(null,innerArray); | |
| }); | |
| } | |
| ----Reverse a string---- | |
| function reverseString(str) { | |
| return str.length ? reverseString(str.substring(1))+str.charAt(0) : ""; | |
| } | |
| ----Roman numeral converter---- | |
| function convert(num) { | |
| return Array.prototype.reduce.call(num.toString(),function(prev,curr,index,array){ | |
| return prev.concat([[0],[1],[1,1],[1,1,1],[1,5],[5],[5,1],[5,1,1],[5,1,1,1],[1,10]][curr].map(function(elem){ | |
| return elem*Math.pow(10,array.length-1-index); | |
| })); | |
| },[]).map(function(elem){ | |
| return ['M', 'D', 'C', 'L', 'X', 'V', 'I'][[1000,500,100,50,10,5,1].indexOf(elem)]; | |
| }).join(""); | |
| } | |
| ----Seek and destroy---- | |
| function destroyer(arr) { | |
| return arr.filter(function(val) { | |
| return Array.prototype.indexOf.call(this,val,1)===-1; | |
| },arguments); | |
| } | |
| ----Sorted union---- | |
| function unite(arr1, arr2, arr3) { | |
| return Array.prototype.reduce.call(arguments, function(prev,curr){ | |
| return prev.concat(curr).filter(function(val, index, array){ | |
| return array.indexOf(val)===index; | |
| }); | |
| }); | |
| } | |
| ----Steamroller---- | |
| function steamroller(arr) { | |
| return arr.length ? (Array.isArray(arr[0]) ? steamroller(arr.shift()) : [arr.shift()]).concat(steamroller(arr)) : []; | |
| } | |
| ----Sum all primes---- | |
| function sumPrimes(num) { | |
| return arguments.length!==1 ? | |
| num>1 ? | |
| sumPrimes(num-1, arguments[1].filter(function(val){return val%num!==0;}).concat(num)) | |
| : | |
| arguments[1].reduce(function(a,b){return a+b;}) | |
| : | |
| num>1 ? sumPrimes(num-1, [num]) : 0; | |
| } | |
| ----Symmetric difference---- | |
| function sym(args) { | |
| return Array.prototype.slice.call(arguments).reduce(function(prev, curr){ | |
| return prev.concat(curr.filter(function(val, index){ | |
| return curr.indexOf(val)===index; | |
| })).filter(function(val,ind,array){ | |
| return array.indexOf(val)===array.lastIndexOf(val); | |
| }); | |
| }, []); | |
| } | |
| ----Title case a sentence---- | |
| function titleCase(str) { | |
| return str.split(' ').map(function(val){ | |
| return val.charAt(0).toUpperCase() + val.substr(1).toLowerCase(); | |
| }).join(' '); | |
| } | |
| ----Where art thou---- | |
| function where(collection, source) { | |
| return collection.filter(function(obj){ | |
| return Object.keys(source).every(function(key){ | |
| return obj[key]===source[key]; | |
| }); | |
| }); | |
| } | |
| ----Where do I belong---- | |
| function where(arr, num) { | |
| return arr.reduce(function(prev, curr){ | |
| return curr<num ? prev+1 : prev; | |
| },0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome! ๐