Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 29, 2021 11:53
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/f265b70222b67840023e40f2aafc2f9a to your computer and use it in GitHub Desktop.
Save codecademydev/f265b70222b67840023e40f2aafc2f9a to your computer and use it in GitHub Desktop.
Codecademy export
// Returns a random DNA base
const returnRandBase = () => {
const dnaBases = ['A', 'T', 'C', 'G']
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 (number, array) {
return {
specimenNum: number,
dna: array,
mutate() {
const randomNumb = Math.floor(Math.random()*15);
const pickRandBase = this.dna[randomNumb];
var newBase = "";
do {
newBase = returnRandBase();
} while (pickRandBase === newBase);
this.dna[randomNumb] = newBase;
},
compareDNA(input) {
let DNACounter = 0;
for (i = 0; i < 15; i++) {
if (this.dna[i] === input["dna"][i]) {
DNACounter++;
}
}
const percent = Math.round(DNACounter / 15 * 100);
console.log(`Specimen # ${this.specimenNum} is ${percent}% identical with specimen # ${input["specimenNum"]}.`)
return percent;
},
willLikelySurvive() {
CGCounter = 0;
for (i = 0; i < 15; i++) {
if (this.dna === "C" || this.dna === "G") {
CGCounter++;
}
}
return (CGCounter / 15) >= 0.6;
},
complementStrand() {
const complementDNA = [];
for (let base of this["dna"]) {
switch (base) {
case "A":
complementDNA.push("T");
break;
case "T":
complementDNA.push("A");
break;
case "C":
complementDNA.push("G");
break;
case "G":
complementDNA.push("C");
break;
default:
complementDNA.push("unidentified");
}
}
return complementDNA;
}
}
}
// Make 30 Organisms
const pAequor = [];
for (i = 1; i < 31; i++) {
pAequor.push(pAequorFactory(i, mockUpStrand()));
}
// just to see what one looks like
console.log(pAequor[0])
// two most related
let mostRelatedPercent = 0;
let mostRelatedSpec1 = 0;
let mostRelatedSpec2 = 0;
for (let i = 0; i < 30; i++) {
for (let j = 0; j < 30; j++) {
if (i !== j) {
if (pAequor[i].compareDNA(pAequor[j]) > mostRelatedPercent) {
mostRelatedPercent = pAequor[i].compareDNA(pAequor[j]);
mostRelatedSpec1 = pAequor[i].specimenNum;
mostRelatedSpec2 = pAequor[j].specimenNum;
}
} else { continue; }
}
}
console.log(`Specimen #${mostRelatedSpec1} and #${mostRelatedSpec2} are the most identical with ${mostRelatedPercent}%.`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment