Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 6, 2021 12:04
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/9e519b066ef90ffec5d8840d8d91fdef to your computer and use it in GitHub Desktop.
Save codecademydev/9e519b066ef90ffec5d8840d8d91fdef 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 strand 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,
mutate(){
const i = Math.floor(Math.random() * 15);
let tBase = dna[i];
while (this.dna[i] === tBase) {tBase = returnRandBase()};
this.dna[i] = tBase;
return dna;
},
compareDNA(pAequorObj){
let count = 0;
for (let i = 0; i < this.dna.length; i++){
if (this.dna[i] === pAequorObj.dna[i]){count++};
}
const percent = (count / 15) * 100;
console.log(`Specimen ${this.specimenNum} and specimen ${pAequorObj.specimenNum} have ${percent} % DNA in common.`);
},
willLikelySurvive(){
let countC = 0, countG = 0;
const dnaLen = this.dna.length;
for (let i = 0; i < dnaLen; i++){
if (this.dna[i] === 'C'){
countC++;
} else if (this.dna[i] === 'G'){
countG++;
};
};
return ((countC / dnaLen) >= 0.6) || ((countG / dnaLen) >= 0.6);
}
}
}
function create30pAequorObjects(){
let arrayOf30pAequor = [];
let count30 = 0;
testPAequor = {};
while (count30 < 30){
testPAequor = pAequorFactory(count30, mockUpStrand());
if (testPAequor.willLikelySurvive()){
arrayOfPAequor.push(testPAequor);
count30++;
};
};
return arrayOf30pAequor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment