Skip to content

Instantly share code, notes, and snippets.

View evolutionxbox's full-sized avatar
👀
Trying my hardest

Jonathan Cousins evolutionxbox

👀
Trying my hardest
View GitHub Profile
@evolutionxbox
evolutionxbox / es2015-class-compose.js
Last active May 3, 2016 10:07
Compositional Inheritance using ES2015 classes
class Openable {
constructor(isOpen = false) {
this._isOpen = isOpen;
}
get isOpen() {
return this._isOpen + ' is stupid.';
}
function ClosedBracketWord(w='w') {
return !w[19] && w.split('').every((l, i) => 122-(l.charCodeAt()-97) === w.charCodeAt(w.length-1-i));
}
ClosedBracketWord('abiryz'); // true
ClosedBracketWord('abixyz'); // false
ClosedBracketWord('ABCXYZ'); // false
ClosedBracketWord(''); // true
@evolutionxbox
evolutionxbox / TicTacToe.js
Last active April 6, 2016 13:35
You are given a 3 × 3 Tic-tac-toe board stored as a two-dimensional array. Find out the winner of the game, or return ' ' if there is no winner.
TicTacToe = (board) => {
let winbits = [7,56,73,84,146,273,292,448];
board = Array.prototype.concat.apply([], board);
query = player => {
num = 0;
board.forEach((cell, i) => {
var i = (board.length-i)-1;
num += (cell === player)<<i;
});
@evolutionxbox
evolutionxbox / CanMakePalindrome.js
Last active April 6, 2016 15:18
Given a string `s`, determine whether or not it is possible to rearrange the letters in the string to make a palindrome. (Post manual "uglify")
CanMakePalindrome = (s, b, o, t) => {
t = s.length % 2;
o = [...s].filter(l => {
b = s.split(l);
s = b.join``;
return (b.length - 1)%2;
});
return t ? (o.length === 1) : (o.length === 0);
}
@evolutionxbox
evolutionxbox / counter-module.js
Created May 3, 2016 09:54
A simple counter object, which uses closures instead of "this".
var Counter = (function counter(i) {
increment = function increment() {
++i;
};
decrement = function decrement() {
--i;
};
@evolutionxbox
evolutionxbox / rgba-calc.js
Created May 3, 2016 10:04
Calculate the average alpha needed to get a foreground colour to be as close to a target colour, when used with transparency above a background colour.
var rgb = (function(red, blue, green) {
return {
red: red,
blue: blue,
green: green
};
});
var rgb1 = rgb(0, 0, 0); // colour to use
var rgb2 = rgb(255, 255, 255); // background colour
@evolutionxbox
evolutionxbox / object-composition.html
Created May 10, 2016 11:21
Object Compositional using Closures
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Object Composition</title>
</head>
<body>
<h1>
<a class="first" href="#">First</a>
<a class="second" href="#">Second</a>
@evolutionxbox
evolutionxbox / fontLoaded.js
Created May 20, 2016 16:45
Font Loaded fires an event when "FontAwesome" has been loaded.
// Font Loaded
(function fontLoaded() {
var retries = 300;
function checkReady() {
var canvas,
context;
retries -= 1;
canvas = document.createElement('canvas');
canvas.width = 20;
@evolutionxbox
evolutionxbox / document.cookies.js
Last active June 7, 2016 16:29
document.cookies returns an object containing key value pairs of all the document's cookies.
// document.cookies
// getter property accessor
// returns Object
//
// Examples:
// var cookies = document.cookies;
// var mycookie = document.cookies.mycookie;
if (typeof Document.prototype.cookies == 'undefined') {
Object.defineProperty(Document.prototype, "cookies", {
@evolutionxbox
evolutionxbox / customElement.js
Created July 29, 2016 10:28
Exploring Custom Elements using the RMP.
var MyElement = new function MyElement() {
var obj = Object.create(HTMLElement.prototype);
var self = {
createdCallback: function createdCallback() {
console.log('createdCallback', this);
},
attachedCallback: function attachedCallback() {
console.log('attachedCallback', this);
},