Skip to content

Instantly share code, notes, and snippets.

View platypusrex's full-sized avatar

Frank Cooke platypusrex

View GitHub Profile
{
"hello": "world"
}
@platypusrex
platypusrex / nested-list.html
Created July 4, 2019 00:25
Infinitely Nested List:
<ul>
<li>Item 1
<ul>
<li>Item 1.1
<ul>
<li>Item 1.1.1</li>
<li>Item #1.1.2
<ul>
<li>Item 1.1.2.1</li>
</ul>
@platypusrex
platypusrex / findOdd.js
Created April 8, 2018 17:08
Given an array, find the int that appears an odd number of times.
const findOdd = (arr) => arr.reduce((a,b) => a ^ b);
findOdd([1,1,2,-2,5,2,4,4,-1,-2,5])
@platypusrex
platypusrex / end.js
Created October 15, 2015 15:27
Javascript - Check if a string (first argument) ends with the given target string (second argument).
function end(str, target) {
return str.substr(str.length - target.length) === target ? true : false;
}
end('platypus', 's');
end('I\'m pretty sure this thing is on', 'on');
@platypusrex
platypusrex / largest.js
Created October 15, 2015 15:24
Javascript - Return an array consisting of the largest number from each provided sub-array
function largest(arr) {
return arr.map(function(val){
return val.reduce(function(prev, current){
return current > prev ? current : prev;
});
});
}
console.log(largest([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
@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");
@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 / 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 / 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 / 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)
});