Skip to content

Instantly share code, notes, and snippets.

//Accelerometer Javascript example by @karlwestin, Nerd Communications, Berlin
// Firefox, works on desktop too
window.addEventListener("MozOrientation", updateText, true);
// Mobile Safari
window.addEventListener("devicemotion", updateText, true);
function updateText(e) {
@karlwestin
karlwestin / .gitconfig
Created May 23, 2011 13:34
Make git diff ignore whitespace and don't try to open jpgs and shit
# this can be put in [repo]/.git/config for local settings
# or ~/.gitconfig for global settings
# create a difftool "nodiff" that just returns true
# this path is for Mac. On linux it's /bin/true i guess
[diff "nodiff"]
command = /usr/bin/true
# make git ignore white space differences, many different possibilites here
@karlwestin
karlwestin / node-firmata-attempt.js
Created July 6, 2011 23:14
My short node-js-firmata attempt
// On Arduino Uno 2.2
// Using Examples > Firmata > StandardFirmata_2_2_forUNO_0_3
var firmata = require("./firmata/lib/firmata");
var board = new firmata.Board("/dev/tty.usbmodem621");
board; // logs board object, board.pins is empty
board.digitalWrite(2, 1); // generates error can't write value to undefined
@karlwestin
karlwestin / gist:1183781
Created August 31, 2011 15:08
Image changing, then changing back, then changing the next image
$(document).ready(changer);
function changer() {
var imgnum = Math.floor(15 * Math.random());
var time = Math.floor(5000 * Math.random());
var $img = $('.project_thumb .cardimgcrop img').eq(imgnum);
var newsrc = "http://yaplog.jp/cv/omb-master/img/449/noise_p.gif";
$img.css({
'width': $img.css('width'),
@karlwestin
karlwestin / gist:1404606
Created November 29, 2011 12:12
Generating 10000 random codes in ruby
require 'digest/sha1'
(1..10000).map {|n| (Digest::SHA1.hexdigest (n*rand).to_s)[1..8]}
@karlwestin
karlwestin / gist:1690875
Created January 27, 2012 20:55
roman/arabic conversion code kata
// while reading: http://compositecode.com/2011/09/27/code-katas-kicking-off-with-a-little-javascript/
function romanToArabic(roman) {
var letters = ["M", "D", "C", "L", "X", "V", "I"],
values = { M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 },
sum = 0;
for(var i=0; i<roman.length; i++) {
if (typeof roman[i+1]!= "undefined" && letters.indexOf(roman[i]) > letters.indexOf(roman[i+1]) ) {
sum += values[roman[i+1]] - values[roman[i]];
i++;
@karlwestin
karlwestin / gist:1690886
Created January 27, 2012 20:58
My first take on roman-to-arabic code kata
function romanToArabic(roman) {
var ones = 0,
fives = 0,
tens = 0,
fifties = 0,
result = 0,
lCount = roman.split(/L/g).length -1,
xCount = roman.split(/X/g).length -1,
vCount = roman.split(/V/g).length -1,
@karlwestin
karlwestin / gist:1707413
Created January 30, 2012 23:14
The Bowling Kata, written in javascript, "score" function
// This is the implementation from https://github.com/zachleat/JavaScript-Code-Katas
Game.prototype.score = function()
{
var points = 0,
frameIndex = 0;
for(var frame = 0; frame<10; frame++) {
if(this.isStrike(frameIndex)) {
points += this.strikeBonus(frameIndex);
@karlwestin
karlwestin / gist:1707420
Created January 30, 2012 23:15
The bowling Kata, my version with array.shift();
var score = function() {
var score = 0;
for(var i = 0; i < 10; i++) {
if(rolls[0] === 10)
score += rolls.shift() + rolls[0] + rolls[1];
else if(rolls[0] + rolls[1] === 10)
score += rolls.shift() + rolls.shift() + rolls[0];
else
score += rolls.shift() + rolls.shift();
@karlwestin
karlwestin / gist:1707528
Created January 30, 2012 23:34
Shift, unshift, push, pop in javascript
var arr = [1,2,3,4,5,6,7];
var first = arr.shift(); // first = 1, arr = [2,3,4,5,6,7]
arr.unshift(0); // arr = [0,2,3,4,5,6,7]
arr.push(8); // arr = [0,2,3,4,5,6,7,8]
var last = arr.pop() // last = 8, arr = [0,2,3,4,5,6,7]