-
-
Save codecademydev/e109d30763494fd7637b27189a9651e9 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 | |
} | |
const pAequorFactory = (number,dnaBases) => { | |
return { | |
specimenNum: number, | |
dna: dnaBases, | |
mutate () { | |
const dnaBases = ['A', 'T', 'C', 'G']; | |
let dnaMutated = Math.floor(Math.random()*15); | |
let indexOfDNAMutated = dnaBases.indexOf(this.dna[dnaMutated]); | |
let prevBases = dnaBases.slice(0,indexOfDNAMutated); | |
let postBases = dnaBases.slice(indexOfDNAMutated+1,dnaBases.length) | |
let candidateDNABases = prevBases.concat(postBases); | |
this.dna[dnaMutated] = candidateDNABases[Math.floor(Math.random()*3)]; | |
return this.dna; | |
}, | |
compareDNA (pAequor) { | |
let sameDNA = 0; | |
for (let i = 0; i < this.dna.length; i++) { | |
if (this.dna[i] === pAequor.dna[i]) { | |
sameDNA++; | |
} | |
} | |
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${(sameDNA/this.dna.length).toFixed(2)*100}% DNA in common`) | |
}, | |
willLikelySurvive () { | |
let countOfCAndG = 0; | |
for (let d = 0; d < this.dna.length; d++) { | |
if (this.dna[d] === 'C' || this.dna[d] === 'G') { | |
countOfCAndG++; | |
} | |
} | |
return (countOfCAndG/this.dna.length) >= 0.6; | |
} | |
} | |
} | |
let specimenNumber = 1; | |
let survivorsCount = 0; | |
let survivors = []; | |
while (survivorsCount < 30) { | |
let pAequorCaught = pAequorFactory(specimenNumber,mockUpStrand()); | |
if (pAequorCaught.willLikelySurvive()) { | |
survivors.push(pAequorCaught); | |
survivorsCount++; | |
} | |
specimenNumber++; | |
} | |
console.log(survivors) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment