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
| Some FCC one-liner solutions |
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
| function binaryAgent(str) { | |
| return str.split(" ").map(function(elem){ | |
| return String.fromCharCode(parseInt(elem,2)); | |
| }).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 boo(bool) { | |
| return !!bool===bool; | |
| } |
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 rot13(str) { | |
| return str.split("").map(function(char){ | |
| return ((cc=char.charCodeAt(0))>64 && cc<91) ? String.fromCharCode(cc%26+65) : char; | |
| }).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 palindrome(str) { | |
| return str.replace(/[\W_]/g,"").toLowerCase().split("").every(function(elem,index,array){ | |
| return elem===array[array.length-1-index]; | |
| }); | |
| } |
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 chunk(arr, size) { | |
| return arr.length ? [arr.splice(0,size)].concat(chunk(arr,size)):[]; | |
| } |
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 end(str, target) { | |
| return str.slice(-target.length)===target; | |
| } |
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 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"); | |
| } |
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 diff(arr1, arr2) { | |
| return arr1.concat(arr2).filter(function(val, index, array){ | |
| return array.indexOf(val)===array.lastIndexOf(val); | |
| }); | |
| } |
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 pair(str){ | |
| return Array.prototype.map.call(str, function(val){ | |
| return [val,{A:'T', T: 'A', C: 'G', G: 'C'}[val]]; | |
| }); | |
| } |
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 drop(arr, func) { | |
| return (arr.length && !func(arr[0])) ? drop(arr.slice(1),func) : arr; | |
| } |
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 every(collection, pre) { | |
| return collection.every(function(obj){ | |
| return obj[pre]; | |
| }); | |
| } |
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 checkCashRegister(price, cash, cid) { | |
| return (changeLeft = cash-price) && (change = cid.reverse().reduce(function(prev,curr){ | |
| return (amount = (Math.min(changeLeft,curr[1])+(LAMBDA=0.0000001))/ | |
| (note = {"ONE HUNDRED":100,"TWENTY":20,"TEN":10,"FIVE":5,"ONE":1,"QUARTER":0.25, | |
| "DIME":0.10,"NICKEL":0.05,"PENNY":0.01}[curr[0]])>>0) ? | |
| prev.push([curr[0],amount*note]) && (curr[1] -= amount*note)+1 && (changeLeft -= amount*note)+1 && prev | |
| : | |
| prev; | |
| },[])) && | |
| changeLeft>LAMBDA ? | |
| "Insufficient Funds" | |
| : | |
| cid.every(function(elem){return elem[1]<LAMBDA;}) ? "Closed" : change; | |
| } |
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 bouncer(arr) { | |
| return arr.filter(function(val){return val;}); | |
| } |
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 findLongestWord(str) { | |
| return str.split(' ').reduce(function(prev, curr) { | |
| return Math.max(prev, curr.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 find(arr, func) { | |
| return arr.length ? (func(arr[0]) ? arr[0] : find(arr.slice(1),func)) : 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
| function updateInventory(old, del) { | |
| return del.reduce(function(prev,curr){ | |
| return ((ind = (itemNames = old.reduce(function(prev,curr){ | |
| return prev.concat(curr[1]); | |
| },[])).indexOf(curr[1]))===-1) ? | |
| itemNames.push(curr[1])&&prev.concat([curr]) | |
| : | |
| (prev[ind][0] += curr[0])&&prev; | |
| },old).sort(function(a,b){ | |
| return a[1]===b[1] ? 0 : | |
| a[1] < b[1] ? -1 : 1; | |
| }); | |
| } |
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 fearNotLetter(str) { | |
| if (str.length>1) return str.charCodeAt(1)===str.charCodeAt(0)+1 ? fearNotLetter(str.slice(1)) : String.fromCharCode(str.charCodeAt(0)+1); | |
| } |
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 mutation(arr) { | |
| return arr[1].toLowerCase().split("").every(function(val){ | |
| return arr[0].toLowerCase().indexOf(val)>-1; | |
| }); | |
| } |
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 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); | |
| } |
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 lookUp(firstName, prop){ | |
| return contacts.reduce(function(prev,curr){ | |
| return curr.firstName===firstName ? | |
| curr.hasOwnProperty(prop) ? curr[prop] : "No such property" | |
| : | |
| prev; | |
| },"No such contact"); | |
| } |
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 repeat(str, num) { | |
| return num>0 ? str+repeat(str,num-1) : ""; | |
| } |
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 largestOfFour(arr) { | |
| return arr.map(function(innerArray){ | |
| return Math.max.apply(null,innerArray); | |
| }); | |
| } |
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 reverseString(str) { | |
| return str.length ? reverseString(str.slice(1))+str.charAt(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 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(""); | |
| } |
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 destroyer(arr) { | |
| return arr.filter(function(val) { | |
| return Array.prototype.indexOf.call(this,val,1)===-1; | |
| },arguments); | |
| } |
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 smallestCommons(arr) { | |
| return arguments.length===1 ? | |
| Array.apply(null, {length: Math.max(arr[0],arr[1])-Math.min(arr[0],arr[1])+1}) | |
| .map(function(value, index){ | |
| return Math.min(arr[0],arr[1]) + index; | |
| }) | |
| .reduce(function(prev,curr){return prev*curr / smallestCommons(prev,curr);}) | |
| : | |
| arguments[1]===0 ? arguments[0] : smallestCommons(arguments[1], arguments[0]%arguments[1]); | |
| } |
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 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; | |
| }); | |
| }); | |
| } |
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 steamrollArray(arr) { // recursive | |
| return Array.isArray(arr) ? Array.prototype.concat.apply([],arr.map(steamrollArray)):arr; | |
| } | |
| function steamrollArray(arr) { // non-recursive | |
| while (arr.some(Array.isArray)) { | |
| arr = Array.prototype.concat.apply([],arr); | |
| } | |
| return arr; | |
| } |
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 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; | |
| } |
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 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); | |
| }); | |
| }, []); | |
| } |
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 titleCase(str) { | |
| return str.split(' ').map(function(val){ | |
| return val.charAt(0).toUpperCase() + val.substr(1).toLowerCase(); | |
| }).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 where(collection, source) { | |
| return collection.filter(function(obj){ | |
| return Object.keys(source).every(function(key){ | |
| return obj[key]===source[key]; | |
| }); | |
| }); | |
| } |
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 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! ๐