-
-
Save codecademydev/c535708f634a2dbff8eea7e72a4c59c3 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
// pila Aequor factory | |
const pAequorFactory = (count) => { | |
let dna = mockUpStrand(); | |
return {specimenNum:count, | |
dna:dna, | |
mutate () { | |
let i = Math.floor(Math.random()*this.dna.length); | |
let letter = this.dna[i]; | |
let newLetter = letter; | |
while (letter === newLetter) { | |
newLetter = returnRandBase(); | |
} | |
this.dna[i] = newLetter; | |
}, | |
compareDNA(pAequor) { | |
let same = 0; | |
for(let i=0; i<pAequor.dna.length; i++){ | |
if(pAequor.dna[i] === this.dna[i]) { | |
same+=1; | |
} | |
} | |
let percentSame = Math.floor(same/pAequor.dna.length*100); | |
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${percentSame}% DNA in common`); | |
}, | |
// check for survival by counting the ratio of 'C' and 'G' to total | |
willLikelySurvive() { | |
let count_CG = this.dna.reduce((acc, letter)=>{ | |
if(letter === 'C' || letter === 'G'){ | |
return ++acc; | |
} else { | |
return acc; | |
} | |
}, 0); | |
let ratio = count_CG/this.dna.length; | |
//console.log(ratio); | |
return ratio >= 0.6; | |
} | |
} | |
}; | |
const mutant1 = pAequorFactory(1); | |
const mutant2 = pAequorFactory(2); | |
//console.log(mutant1.dna); | |
//mutant1.mutate(); | |
console.log(mutant1.dna); | |
console.log(mutant2.dna); | |
mutant1.compareDNA(mutant2); | |
console.log(mutant1.willLikelySurvive()); | |
console.log(mutant2.willLikelySurvive()); | |
let luckyMutants = []; | |
let mutantCount = 0; | |
while (luckyMutants.length<=30) { | |
let mutant = pAequorFactory(mutantCount); | |
if (mutant.willLikelySurvive()) luckyMutants.push(mutant); | |
mutantCount++; | |
} | |
console.log(mutantCount); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment