Skip to content

Instantly share code, notes, and snippets.

View platypusrex's full-sized avatar

Frank Cooke platypusrex

View GitHub Profile
@platypusrex
platypusrex / reverseStr.js
Last active October 15, 2015 04:06
Javascript - Reverse String
function reverseString(str) {
return str.split('').reverse().join('');
}
reverseString('hello');
@platypusrex
platypusrex / palindrone.js
Last active October 15, 2015 04:05
Javascript - Check for Palindrones
function palindrome(str) {
return str.replace(/\W|_/gi, '').toLowerCase() === str.split('').reverse().join().replace(/\W|_/gi, '').toLowerCase() ? true : false;
}
palindrone('race car');
@platypusrex
platypusrex / factorial.js
Created October 15, 2015 04:07
Javascript - Factorialize any number
function factorialize(num) {
if(num === 0){
return 1;
}
return num * factorialize(num - 1);
}
factorialize(5);
@platypusrex
platypusrex / closure.js
Created October 15, 2015 04:15
Javascript - Arguments and Closures
function add() {
if(arguments.length > 1){
return Array.from(arguments).every(function(val){
return typeof val === 'number';
}) ? Array.from(arguments).reduce(function(prev, current){
return prev + current
}) : undefined;
}else if(typeof arguments[0] === 'number'){
var x = arguments[0];
return function(y){
@platypusrex
platypusrex / binary.js
Created October 15, 2015 04:17
Javascript - Binary to Text Conversion
function binaryAgent(str) {
return str.split(' ').map(function(val){
return String.fromCharCode(parseInt(val, 2));
}).join('').replace(',', ' ');
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
@platypusrex
platypusrex / optimusPrime.js
Created October 15, 2015 04:18
Javascript - Sum all prime numbers
function sumPrimes(num) {
return Array.apply(0, Array(num + 1))
.map(function(x, y){
return y
}).filter(function (i){
return (i > 1) && Array
.apply(0, Array(1 + ~~Math.sqrt(i)))
.every(function(x, y){
return (y < 2) || (i % y !== 0)
});
@platypusrex
platypusrex / flatten.js
Created October 15, 2015 04:20
Javascript - Flatten any nested array (regardless of nesting)
function steamroller(arr) {
// I'm a steamroller, baby
arr = Array.prototype.concat.apply([], arr);
return arr.some(Array.isArray) ? steamroller(arr) : arr;
}
steamroller([1, [2], [3, [[4]]]]);
@platypusrex
platypusrex / union.js
Last active October 15, 2015 04:27
Javascript - Take 2 or more arrays and return an array of unique value in the original order
function unite(arr1, arr2, arr3) {
return Array.from(arguments).reduce(function(a, b){
return a.concat(b).filter(function(val,pos,arr){
return arr.indexOf(val) === pos;
});
});
}
unite([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]);
@platypusrex
platypusrex / differt.js
Created October 15, 2015 04:32
Javascript - Compare two arrays and return a new array with any items not found in both of the original arrays.
function diff(arr1, arr2) {
return newArr = arr1.filter(function(val){
return arr2.indexOf(val) === -1;
}).concat(arr2.filter(function(val){
return arr1.indexOf(val) === -1;
}));
}
diff(['diorite', 'andesite', 'grass', 'dirt', 'pink wool', 'dead shrub'], ['diorite', 'andesite', 'grass', 'dirt', 'dead shrub']);
@platypusrex
platypusrex / titleCase.js
Created October 15, 2015 14:50
Javascript - Title case any phrase
function titleCase(str) {
return str.split(' ').map(function(val){
return val.charAt(0).toUpperCase() + val.slice(1).toLowerCase();
}).join(' ');
}
titleCase("sHoRt AnD sToUt");