Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 18, 2020 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/78d51e9b70c002381a6b432d5defc9c8 to your computer and use it in GitHub Desktop.
Save codecademydev/78d51e9b70c002381a6b432d5defc9c8 to your computer and use it in GitHub Desktop.
Codecademy export
// Returns a random DNA base
const dnaBases = ["A", "T", "C", "G"];
const returnRandBase = () => {
return dnaBases[Math.floor(Math.random() * 4)];
};
// Returns a random single stand of DNA containing 15 bases
const mockUpStrand = () => {
const newStrand = [];
for (let i = 0; i < 15; i++) {
newStrand.push(returnRandBase());
}
return newStrand;
};
function pAequorFactory(num, arr) {
return {
specimenNum: num,
dna: arr,
mutate() {
let indexToMutate = Math.floor(Math.random() * this.dna.length);
let newElement;
do {
newElement = returnRandBase();
} while (newElement === this.dna[indexToMutate]);
this.dna[indexToMutate] = newElement;
return this.dna;
},
compareDNA(obj) {
let identicalProperties = 0;
this.dna.forEach((element, index) => {
if (obj.dna[index] === element) {
identicalProperties++;
}
});
console.log(
`Captain's log: \nSpecies no. ${this.specimenNum} have DNA: ${
this.dna
}\nSpecies no. ${obj.specimenNum} have DNA: ${
obj.dna
}\nIdentitcal properties = ${(
(identicalProperties / this.dna.length) *
100
).toFixed(2)} %`
);
},
willLikelySurvive() {
let surivvalInstance = 0;
this.dna.forEach((element) => {
if (element === "C" || element === "G") {
surivvalInstance++;
}
});
return surivvalInstance / this.dna.length >= 0.6 ? true : false;
},
complementStrand() {
let complementedDNA = [];
this.dna.forEach((element) => {
let append;
switch (element) {
case "A":
append = "T";
break;
case "T":
append = "A";
break;
case "C":
append = "G";
break;
case "G":
append = "C";
break;
}
complementedDNA.push(append);
});
return complementedDNA;
},
compareDNAreturn(obj) {
let identicalProperties = 0;
this.dna.forEach((element, index) => {
if (obj.dna[index] === element) {
identicalProperties++;
}
});
return (identicalProperties / this.dna.length) * 100;
},
};
}
let combinationsWhichSurvive = [];
for (i = 0; i <= 30; i++) {
let tempObj = {};
do {
tempObj = pAequorFactory(i, mockUpStrand());
} while (!tempObj.willLikelySurvive());
combinationsWhichSurvive[i] = tempObj;
}
let comparisonResults = [];
combinationsWhichSurvive.forEach((element, index, array) => {
for (i = index; i < array.length; i++) {
if (array[i].specimenNum != element.specimenNum) {
comparisonResults.push({
pAequor1: element,
pAequor2: array[i],
result: element.compareDNAreturn(array[i]).toFixed(2),
});
}
}
});
console.log(comparisonResults.length);
let highestComparison = comparisonResults.reduce((prevValue, currentValue) => {
return prevValue.result > currentValue.result ? prevValue : currentValue;
});
console.log(
`The most related instances of pAequor are: \nspecimen no. ${
highestComparison.pAequor1.specimenNum
} with DNA strands: \n ${highestComparison.pAequor1.dna.join(
" "
)}\nand nspecimen no. ${
highestComparison.pAequor2.specimenNum
} with DNA strands: \n ${highestComparison.pAequor2.dna.join(
" "
)}.\nThe relativiy rate is at ${highestComparison.result} %`
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment