Last active
March 16, 2017 16:19
-
-
Save devNoiseConsulting/53174d82359185e7ed25c163a5c53fa3 to your computer and use it in GitHub Desktop.
Hamming Distance - PhillyDev Slack #daily_programmer - 20170307
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Calculate the Hamming Distance between 2 DNA strands. | |
*/ | |
const dna1 = "GAGCCTACTAACGGGAT"; | |
const dna2 = "CATCGTAATGACGGCCT"; | |
function hammingDistance1(strand1, strand2) { | |
let count = 0; | |
if (strand1 && strand2) { | |
if (strand1.length == strand2.length) { | |
strand1 = strand1.split(''); | |
strand2 = strand2.split(''); | |
strand1.forEach(function(point, i) { | |
if (point !== strand2[i]) { | |
count++; | |
} | |
}); | |
} else { | |
console.log("WARNING: DNA strands should be the same length."); | |
} | |
} else { | |
console.log("WARNING: Need 2 strands of DNA to test."); | |
} | |
return count; | |
} | |
function hammingDistance2(strand1, strand2) { | |
let count = 0; | |
if (strand1 && strand2) { | |
count = strand1.length; | |
if (strand1.length == strand2.length) { | |
for (let i = 0; i < strand1.length; i++) { | |
if (strand1.charAt(i) == strand2.charAt(i)) { | |
count--; | |
} | |
} | |
} else { | |
console.log("WARNING: DNA strands should be the same length."); | |
} | |
} else { | |
console.log("WARNING: Need 2 strands of DNA to test."); | |
} | |
return count; | |
} | |
hammingDistance = hammingDistance1; | |
let count = hammingDistance(dna1, dna2); | |
console.log(count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment