Skip to content

Instantly share code, notes, and snippets.

View mlms13's full-sized avatar
⚗️

Michael Martin mlms13

⚗️
View GitHub Profile
@mlms13
mlms13 / riskroll.js
Last active August 29, 2015 14:26
Attacker vs Defender with big armies in risk
function roll() {
return Math.ceil(Math.random() * 6);
}
function compare() {
var one = [roll(), roll(), roll()].sort(function (a, b) { return b-a }).slice(0,2);
var two = [roll(), roll()].sort(function (a, b) { return b-a });
var score = 0;
one.forEach(function (item, index) {
@mlms13
mlms13 / Ferble.js
Last active August 29, 2015 14:25
It's a ferble.
function splitIntoGroups(chunks, str) {
var splitAt = str.charAt(0) === '"' ?
// if the string starts with a quote at this point,
// find the next quote or the end of the string
str.indexOf('"', 1) > -1 ? str.indexOf('"', 1) : str.length :
// otherwise, break at the next space
str.indexOf(' ', 1) > -1 ? str.indexOf(' ', 1) : str.length,
word = str.substring(0, splitAt).replace('"', '').trim();
@mlms13
mlms13 / cycle.css
Created July 10, 2015 19:03
Jeet cycling
li {
background: #ddd;
height: 40px;
margin-top: 5px;
margin-bottom: 5px;
}
@media screen and (min-width: 640px) {
li {
*zoom: 1;
float: left;
@mlms13
mlms13 / upgrades.js
Last active January 24, 2017 21:32
List unpurchased upgrades in Cookie Clicker
// I rewrite these way too often, so here they are:
// upgrades
Game.UpgradesById
.filter(function (up) {
return !up.bought && up.pool != "prestige" && up.pool != "toggle" && up.pool != "debug";
})
.map(_ => _.name);
// achievements
@mlms13
mlms13 / pro.js
Last active August 29, 2015 14:23
A promise library for professionals.
function Pro(callback) {
this.isResolved;
this.isRejected;
this.thens = [];
this.catches = [];
var resolve = function (data) {
if (this.isResolved || this.isRejected) return;
this.thens.forEach(function (fn) {
fn(data);
@mlms13
mlms13 / main.js
Last active August 29, 2015 14:23
Promises and Events! and Pseudo-Code!
function tellMeWhenThatThingHappens() {
var promise = new Promise(function (resolve, reject) {
window.addEventListener('thatAwesomeThingHappens', function () {
resolve(/* maybe with some data here */);
});
// if it's possible for that awesome thing to fail...
window.addEventListener('refusingToBeAwesome', function () {
reject(/* maybe with an error */);
});
@mlms13
mlms13 / anagram.js
Last active August 29, 2015 14:16
Solve Anagrams
// beautiful
function findAnagram(parent, child) {
// split the parent string into chars and iterate
return parent.split('').map(function (char, index, arr) {
// map each char in parent to an array of substrings with the same length as child.length
// and alphabetize the substrings for easy comparison later
return arr.slice(index, index + child.length).sort().join('');
}).filter(function (subset) {
// compare the alphabetized substring to the alphabetized child
return subset === child.split('').sort().join('');
@mlms13
mlms13 / audit.js
Created February 2, 2015 18:22
Audit Browserify Dependencies
var mdeps = require('module-deps'),
_ = require('lodash'),
deps = [];
var md = mdeps({
transform: 'hbsfy',
global: true
});
md.on('end', function () {
@mlms13
mlms13 / gulpfile.js
Last active December 3, 2015 21:31
Conditionally switch between source maps and minification based on a gulp flag.
// `gulp stylus --prod` will enable "production mode"
// `gulp stylus` will leave the prod flag undefined, so it will do a dev build
var gulp = require('gulp'),
gutil = require('gulp-util'),
prod = gulp.env.prod;
gulp.task('stylus', ['cleancss'], function () {
var stylus = require('gulp-stylus'),
prefix = require('gulp-autoprefixer'),
minify = require('gulp-minify-css');
@mlms13
mlms13 / typecheck.js
Created September 26, 2014 19:23
Why type-checking (even in an environment like Phantom) is virtually impossible in Javascript
var foo = {
filter: function () {}
};
var bar = [];
// return filtered array or object
function filterer(baz) {
return baz.filter();
}