Skip to content

Instantly share code, notes, and snippets.

View clauswitt's full-sized avatar

Claus Witt clauswitt

View GitHub Profile
@clauswitt
clauswitt / Euler9.js
Created July 4, 2011 19:00
Euler9.js
var sys = require('sys');
var Euler9 = function() {
for(var a = 1; a < 1000; a++) {
for(var b = a+1; b < 1000; b++) {
c = Math.sqrt(Math.pow(a,2)+Math.pow(b,2));
if(c==Math.round(c)) {
if(1000===a+b+c) {
sys.puts(a*b*c);
}
}
@clauswitt
clauswitt / ClickToSplit.js
Created July 4, 2011 19:14
Click to Split
function onclick(e) {
var range;
if (document.caretRangeFromPoint) {
range = document.caretRangeFromPoint(e.pageX, e.pageY);
} else if (e.rangeParent) {
range = document.createRange();
range.setStart(e.rangeParent, e.rangeOffset);
}
var textContainer = range.startContainer;
@clauswitt
clauswitt / Stats.js
Created October 13, 2011 20:47
Simple statistics for javascript
function Stats(arr) {
var self = this;
var theArray = arr || [];
//http://en.wikipedia.org/wiki/Mean#Arithmetic_mean_.28AM.29
self.getArithmeticMean = function() {
var sum = 0, length = theArray.length;
for(var i=0;i<length;i++) {
sum += theArray[i];
}
@clauswitt
clauswitt / SimpleStats.js
Created October 19, 2011 07:50
Even simpler statistics
function SimpleStats(optionalInputArray) {
var self = this;
var setSize = 0,
arithmeticMean = 0,
standardDeviation = 0,
pwrSumAvg = 0;
//http://en.wikipedia.org/wiki/Mean#Arithmetic_mean_.28AM.29
var setArithmeticMean = function(x) {
@clauswitt
clauswitt / gist:1303768
Created October 21, 2011 12:53 — forked from jrburke/gist:1262861
Universal (AMD/Node/plain browser) module
/**
* First, better, "set exports/return" option
*/
(function (define) {
//The 'id' is optional, but recommended if this is
//a popular web library that is used mostly in
//non-AMD/Node environments. However, if want
//to make an anonymous module, remove the 'id'
//below, and remove the id use in the define shim.
define('id', function (require) {
@clauswitt
clauswitt / gist:1320840
Created October 27, 2011 20:55 — forked from kennethkalmer/gist:278814
node.js SMTP Server
/*
smtpd.js is SMTP server written for node.js
MIT License
*/
var tcp = require('tcp');
var sys = require('sys');
@clauswitt
clauswitt / nameGenerator.js
Created November 1, 2011 20:27
Unique Filename generator
define('utils/files/nameGenerator', function() {
var count = 0;
var lastId = 0;
this.getName = function(ext) {
var d = new Date;
var f = d.getTime();
if(f == lastId) {
count++;
} else {
@clauswitt
clauswitt / testGenerator.js
Created November 1, 2011 20:37
Using Filename Generator
require(['utils/files/nameGenerator'], function(filenameGenerator) {
for(var i=0;i<100;i++) {
console.log(filenameGenerator.getName('mp'+(i%2+2)));
}
});
@clauswitt
clauswitt / SimplePromise.js
Created November 14, 2011 07:47
A Simplie Promise AMD module
define('SimplePromise', [], function() {
var constructor = function() {
_ = this;
var theHandler = function(response) {};
var theFunction= function() {};
var theThis;
this.then = function(fn, thisObject) {
handler = fn;
@clauswitt
clauswitt / SimplePromiseTest.js
Created November 14, 2011 07:54
SimplePromise Test File
require(['SimplePromise'], function(SimplePromise) {
var p = SimplePromise();
p.promise(function(sleepTime) {
//This function is called by the run call at the end of this file.
var alarm, now = new Date();
var startingMSeconds = now.getTime();
var sleeping = true;
while(sleeping){
alarm = new Date();