Skip to content

Instantly share code, notes, and snippets.

@adriancooney
adriancooney / loop-u-s-v.js
Created June 21, 2014 00:10
Javascript Proxy Madness
loop[0][2][20] = function(i) {
console.log(i);
};
// Output:
// 0
// 2
// 4
// 6
// 8
@adriancooney
adriancooney / Array2D.js
Created April 4, 2012 13:52
Javascript 2D Array
/* Extremely simple definition of a 2D array in Javascript. Javascript doesn't allow for a match-all getter so a length has to be defined. This defines the height or rows of the array but not the width; the width or columns is as big as you want it to be, sugar. For some odd reason, the __defineGetter__ doesn't send the name of the current "get" to the callback (which is silly in my opinion for situations exactly like these) so I had to convert the functions to strings. */
var Array2D = function(l) {
if(!l) throw new Error("Please specify a length for your Array2D");
this.arr = [];
this.definedLength = l;
for(var i = 0; i < l; i++) {
this.__defineGetter__(i, Function("var i = " + i + ";if(!this.arr[i]) this.arr[i] = [];return this.arr[i];"));
}
@adriancooney
adriancooney / sudoku.js
Created April 20, 2012 21:13
Generate a sudoku game
var sudoku = [], iterationCount = 0;
function check(type, pos, num) {
var bool = false;
for(var i = 0; i < 9; i++) {
var tile = (type === 1) ? (pos * 9) + i : //row
(type === 2) ? (i * 9) + pos : null; //Column
(((((pos - (pos % 3))/3) * 3)) * 9) + ((((pos % 3) * 3)) + (i % 3)); //Quadrant
@adriancooney
adriancooney / 2014_2015_CH140_1_1_5.json
Created November 6, 2015 20:16
Exam PDF translated to JSON.
{
"section_a": {
"1": {
"i": {
"content": " Explain using examples why many hard RTS use a cyclic executive approach to scheduling whereas many soft RTS utilise a real-time operating system. [10] "
},
"ii": {
"content": " Using Antilock Braking Systems (ABS) as an example, distinguish between the sample rate and response time of a RTS. [10] "
},
"iii": {
<body>
  <div>
		<header>
			<h1 id="title"><em><a href="/">Hello world!</a></em></h1>
		</header>
		<section>
			<ul>
				<li class="item"><a href="#">Navigation Item</a></li>
 <a href="#">Navigation Item</a>
@adriancooney
adriancooney / newline.js
Created August 27, 2013 17:01
"Native" multiline Javascript strings (with some funky syntax)
var multiline = function(string) {
// Replace the "function() {/*\n" and "\n*/}" with nothing and bam, multiline strings
return string.toString().replace(/(^[^\n]*\n)|(\n\*\/\})/g, "");
};
console.log(multiline(function() {/*
Hello world!
I'm a multiline string!
Tada!
src/data/auth.coffee-112- id: installationId)
src/data/auth.coffee-113- return new Promise (resolve, reject) ->
src/data/auth.coffee-114- projectsDLM.get cacheKey, (err, res) ->
src/data/auth.coffee-115- return reject(err) if err?
src/data/auth.coffee-116- return resolve res if res?
src/data/auth.coffee:117: currentTimestamp = Date.now().toString()
src/data/auth.coffee-118- projectsDLM.set cacheKey, currentTimestamp, (err, res) ->
src/data/auth.coffee-119- return reject(err) if err?
src/data/auth.coffee-120- resolve currentTimestamp
src/data/auth.coffee-121-
@adriancooney
adriancooney / dinosauras.js
Last active February 8, 2017 01:20
Cheat in Chrome's "No network connection" dinosaur game. Paste this in the console when you find yourself on the screen (or disconnect from the internet and attempt to load a page).
const update_ = Runner.instance_.update;
Runner.instance_.update = function() {
if(this.horizon.obstacles.length) {
const nearestObstacle = this.horizon.obstacles[0];
if(nearestObstacle.xPos < 115 && !this.tRex.jumping) {
if(nearestObstacle.yPos <= 50) {
this.tRex.setDuck(true);
setTimeout(this.tRex.setDuck.bind(this.tRex, false), 500);
} else {
this.tRex.startJump(this.currentSpeed);
@adriancooney
adriancooney / App.js
Last active March 17, 2017 14:25
Redux in React.
import React, { Component } from 'react';
import Provider from "./Provider";
import Incrementer from "./Incrementer";
class App extends Provider {
state = { value: 0 };
render() {
const increment = this.dispatch.bind(this, { type: "INCREMENT" });
const decrement = this.dispatch.bind(this, { type: "DECREMENT" });
process.on("unhandledRejection", function(reason, p) {
console.log(reason, p);
console.log("Rejection handled")
new Promise(function(resolve, reject) {
console.log("Second rejection")
reject(new Error("Foo"));
});
});