Skip to content

Instantly share code, notes, and snippets.

@Masd925
Last active April 6, 2017 10:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Masd925/8b1a9b1d37c322827dcd to your computer and use it in GitHub Desktop.
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.
Some FCC one-liner solutions
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;
}
function binaryAgent(str) {
return str.split(" ").map(function(elem){
return String.fromCharCode(parseInt(elem,2));
}).join("");
}
function boo(bool) {
return !!bool===bool;
}
function rot13(str) {
return str.split("").map(function(char){
return ((cc=char.charCodeAt(0))>64 && cc<91) ? String.fromCharCode(cc%26+65) : char;
}).join("");
}
function palindrome(str) {
return str.replace(/[\W_]/g,"").toLowerCase().split("").every(function(elem,index,array){
return elem===array[array.length-1-index];
});
}
function chunk(arr, size) {
return arr.length ? [arr.splice(0,size)].concat(chunk(arr,size)):[];
}
function end(str, target) {
return str.slice(-target.length)===target;
}
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");
}
function diff(arr1, arr2) {
return arr1.concat(arr2).filter(function(val, index, array){
return array.indexOf(val)===array.lastIndexOf(val);
});
}
function pair(str){
return Array.prototype.map.call(str, function(val){
return [val,{A:'T', T: 'A', C: 'G', G: 'C'}[val]];
});
}
function drop(arr, func) {
return (arr.length && !func(arr[0])) ? drop(arr.slice(1),func) : arr;
}
function every(collection, pre) {
return collection.every(function(obj){
return obj[pre];
});
}
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;
}
function bouncer(arr) {
return arr.filter(function(val){return val;});
}
function findLongestWord(str) {
return str.split(' ').reduce(function(prev, curr) {
return Math.max(prev, curr.length);
}, 0);
}
function find(arr, func) {
return arr.length ? (func(arr[0]) ? arr[0] : find(arr.slice(1),func)) : undefined;
}
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;
});
}
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);
}
function mutation(arr) {
return arr[1].toLowerCase().split("").every(function(val){
return arr[0].toLowerCase().indexOf(val)>-1;
});
}
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);
}
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");
}
function repeat(str, num) {
return num>0 ? str+repeat(str,num-1) : "";
}
function largestOfFour(arr) {
return arr.map(function(innerArray){
return Math.max.apply(null,innerArray);
});
}
function reverseString(str) {
return str.length ? reverseString(str.slice(1))+str.charAt(0) : "";
}
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("");
}
function destroyer(arr) {
return arr.filter(function(val) {
return Array.prototype.indexOf.call(this,val,1)===-1;
},arguments);
}
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]);
}
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;
});
});
}
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;
}
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;
}
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);
});
}, []);
}
function titleCase(str) {
return str.split(' ').map(function(val){
return val.charAt(0).toUpperCase() + val.substr(1).toLowerCase();
}).join(' ');
}
function where(collection, source) {
return collection.filter(function(obj){
return Object.keys(source).every(function(key){
return obj[key]===source[key];
});
});
}
function where(arr, num) {
return arr.reduce(function(prev, curr){
return curr<num ? prev+1 : prev;
},0);
}
@abhisekp
Copy link

abhisekp commented Mar 2, 2016

awesome! 😄

@nickdotht
Copy link

Great job 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment