Skip to content

Instantly share code, notes, and snippets.

@devNoiseConsulting
Last active March 16, 2017 16:19
Show Gist options
  • Save devNoiseConsulting/53174d82359185e7ed25c163a5c53fa3 to your computer and use it in GitHub Desktop.
Save devNoiseConsulting/53174d82359185e7ed25c163a5c53fa3 to your computer and use it in GitHub Desktop.
Hamming Distance - PhillyDev Slack #daily_programmer - 20170307
/*
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