Permalink
Browse files

Changed to be node-like; wrote hamming distance method.

  • Loading branch information...
brianloveswords committed Dec 11, 2010
1 parent e9bf43d commit 241e53c912b43a45cbd164bda9e872caab28268d
Showing with 19 additions and 6 deletions.
  1. +15 −4 distance.js
  2. +4 −2 test.js
View
@@ -1,5 +1,16 @@
-String.prototype.distance = (function(){
-
- return function(){console.dir('hi');};
-})();
+var hamming = function(str1, str2){
+ var dist = 0,
+ big = str1,
+ small = str2;
+ if (str2.length > str1.length) {
+ big = str2;
+ small = str1;
+ }
+ dist = big.length - small.length;
+ for (var i=0; i < small.length; i++) {
+ if (small[i] != big[i]) { dist +=1; }
+ }
+ return dist;
+};
+exports['hamming'] = hamming;
View
@@ -1,4 +1,6 @@
var assert = require("assert");
-require("./distance");
+var dist = require("./distance");
-assert.equal(typeof 'awesome'.distance, 'function');
+assert.equal(dist.hamming('awesome', 'aewsome'), 2);
+assert.equal(dist.hamming('a', 'ba'), 2);
+assert.equal(dist.hamming('cab', 'abra'), 4);

0 comments on commit 241e53c

Please sign in to comment.