Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 28, 2020 09:10
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/c2256cbb7247caae9c0820503fa9be82 to your computer and use it in GitHub Desktop.
Save codecademydev/c2256cbb7247caae9c0820503fa9be82 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;
};
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum: specimenNum,
dna: dna,
//The mutate function below changes one base of the dna sequence of the pAequorFactory object
mutate() {
let randomSelector = Math.floor(Math.random() * 15);
let randomBase;
do {
randomBase = returnRandBase();
} while(randomBase === this.dna[randomSelector]);
this.dna[randomSelector] = randomBase;
return this.dna;
},
compareDNA(pForeign) {
let matchCount = 0;
let matchPercent = 0;
for (let i = 0; i < this.dna.length; i++) {
if (pForeign.dna[i] === this.dna[i]) {
matchCount++;
}
}
matchPercent = ((matchCount/15)*100).toFixed(1);
console.log(matchCount);
console.log(matchPercent);
console.log(`Specimen: ${this.specimenNum} and Specimen: ${pForeign.specimenNum} have ${matchPercent} DNA in common`);
},
willLikelySurvive() {
let cBaseCnt = 0;
let gBaseCnt = 0;
for (let i = 0; i < this.dna.length; i++) {
switch(this.dna[i]) {
case 'C':
cBaseCnt++;
break;
case 'G':
gBaseCnt++;
break;
default:
break;
}
}
let cBasePercent = ((cBaseCnt/15)*100);
let gBasePercent = ((cBaseCnt/15)*100);
if (cBasePercent >= 60) {
return true;
}
if (gBasePercent >= 60) {
return true;
}
return false;
}
};
};
const pAequor = [];
for (let i = 0; i < 30; i++) {
pAequor[i] = pAequorFactory(i, mockUpStrand());
}
const pAequorlivable = [];
for (let j = 0; j < pAequor.length; j++){
if(pAequor[j].willLikelySurvive() === true) {
pAequorlivable.push(pAequor[j]);
}
}
if (pAequorlivable.length >= 1) {
console.log(pAequorlivable);
}
else {
console.log('None of the pAequors in the batch of specimens will survive');
}
/*test for the pAequorFactory function below:
console.log('test for the pAequorFactory function below:');
const pAequor1 = pAequorFactory(1, mockUpStrand());
console.log(pAequor1);
*/
/*tests for the mutate function
console.log('tests for the mutate function:');
pAequor1.mutate();
console.log(pAequor1);
*/
/*Tests for the compareDNA method are below:
console.log('Tests for the compareDNA method are below:');
const pAequor2 = pAequorFactory(2, mockUpStrand());
console.log(`pAequor1 dna strand is: ${pAequor1.dna}`);
console.log(`pAequor2 dna strand is: ${pAequor2.dna}`);
pAequor2.compareDNA(pAequor1);
*/
/*Tests for the willLikelySurvive method is below:
console.log('Tests for the .willLikelySurvive method is below:')
console.log(pAequor1.willLikelySurvive());
console.log(pAequor2.willLikelySurvive());
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment