summary refs log tree commit diff stats
path: root/javascript/hamming/hamming.js
diff options
context:
space:
mode:
Diffstat (limited to 'javascript/hamming/hamming.js')
-rw-r--r--javascript/hamming/hamming.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/javascript/hamming/hamming.js b/javascript/hamming/hamming.js
new file mode 100644
index 0000000..4d0908d
--- /dev/null
+++ b/javascript/hamming/hamming.js
@@ -0,0 +1,22 @@
+'use strict';
+
+export const compute = (strand1, strand2) => {
+    if (strand1 === "" && strand2 === "")
+        return 0;
+
+    if (strand1 === "")
+        throw new Error('left strand must not be empty');
+
+    if (strand2 === "")
+        throw new Error('right strand must not be empty');
+
+    if (strand1.length !== strand2.length)
+        throw new Error('left and right strands must be of equal length');
+
+    let distance = 0;
+    for (let idx = 0; idx < strand1.length; idx++)
+        if (strand1[idx] !== strand2[idx])
+            distance++;
+
+    return distance;
+};