Skip to content

Instantly share code, notes, and snippets.

View hoffination's full-sized avatar
🗻
keep climbing

Ben Hofferber hoffination

🗻
keep climbing
View GitHub Profile
@hoffination
hoffination / pileOfCubes.clj
Created March 14, 2016 18:57
Evaluate a pile of cubes
;; Link to KATA: http://www.codewars.com/kata/5592e3bd57b64d00f3000047/train/clojure
;; Your task is to construct a building which will be a pile of n cubes. The cube at the bottom will have a volume of n^3, the cube above will have volume of (n-1)^3 and so on until the top which will have a volume of 1^3.
;; You are given the total volume m of the building. Being given m can you find the number n of cubes you will have to build?
;; The parameter of the function findNb (find_nb, find-nb) will be an integer m and you have to return the integer n such as n^3 + (n-1)^3 + ... + 1^3 = m if such a n exists or -1 if there is no such n.
;; EXAMPLES
;; findNb(1071225) --> 45
@hoffination
hoffination / EventManager.js
Created December 21, 2016 04:57
Dead Simple Event Aggregator
module.exports = (function() {
function EventManager() {
this.events = {};
}
// Will throw if there is no existing event to trigger
EventManager.prototype.trigger = function(msg) {
// grab all arguments passed after the first argument
var args = Array.prototype.splice.call(arguments, 1);
for (var i = 0, len = this.events[msg].length; i < len; i++) {
@hoffination
hoffination / clickToOpenData.js
Created December 30, 2016 22:06
Great way to display downloadable data in a new tab
var obj = {a: 123, b: "4 5 6"};
var container = document.getElementById('container'); // some div on the page
container.addEventListener('click', function() {
var data = "text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(obj));
window.open("data://" + data, "_blank");
})
@hoffination
hoffination / dynamodb-intro.js
Created February 14, 2017 02:28
Introduction to DynamoDB on NodeJS
var AWS = require('aws-sdk');
AWS.config.update({
endpoint: "http://localhost:8000",
region: "us-west-2",
accessKeyId: "myKeyId",
secretAccessKey: "secretKey"
});
var dyn = new AWS.DynamoDB();
var docClient = new AWS.DynamoDB.DocumentClient();
@hoffination
hoffination / bindSetTimeout.js
Created June 7, 2017 18:33
Example of how to use bind to save off variables for the callback within a setTimeout
for(var i = 0; i < 5; i++) {
setTimeout(function(printMe) {
console.log(printMe);
}.bind(null, i), i * 1000);
}
@hoffination
hoffination / microwaveExample.js
Last active June 14, 2017 18:45
'this' example for a microwave
(function() {
"use strict"
const microwave = {
_timer: null,
_eventListener: null,
start: function(timeMS, callback) {
this._eventListener = callback
this._timer = setTimeout((() => {
this._timer = null
@hoffination
hoffination / microwaveImprovedExample.js
Last active June 14, 2017 18:45
'this' within a prototype offers improvements over the previous microwave example
(function() {
"use strict"
function Microwave() {
this.timer = null
this.eventListener = null
}
Microwave.prototype.start = function(timeMS, callback) {
this.eventListener = callback
@hoffination
hoffination / javascriptShadow.js
Last active June 28, 2017 18:06
JavaScript Object Shadowing
(function() {
'use strict';
// Define a generic prototype for our skiiers to inherit
const skiierPrototype = {
name: 'fred',
greet: function() {
return 'Hi, I am ' + this.name
}
}
@hoffination
hoffination / babel_example.html
Last active July 4, 2017 18:47
Babel Example HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Babel Test</title>
<meta name="description" content="Babel Test Site">
<meta name="author" content="Ben Hofferber">
<script src="node_modules/babel-polyfill/dist/polyfill.min.js"></script>
</head>
@hoffination
hoffination / babel_example.js
Created July 4, 2017 18:24
Babel Example JavaScript
(function (window, document) {
"use strict"
const NAME = 'Ben Hofferber'
let div = document.getElementById('appendToMe')
let h1 = document.createElement('h1')
h1.textContent = `Hello ${NAME}!`
div.appendChild(h1)