-
-
Save codecademydev/007d4d3de57c17f60b0f996691267ccd 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; | |
}; | |
function getRandNum(min, max) { | |
return Math.floor(Math.random() * max) + min; | |
} | |
function pAequorFactory(specimenNum, dna) { | |
return { | |
specimenNum, | |
dna, | |
mutate() { | |
const randNum = getRandNum(0, 14); | |
const randBase = this.dna[randNum]; | |
let mutatedBase = null; | |
while (mutatedBase !== randBase) { | |
mutatedBase = returnRandBase(); | |
} | |
this.dna[getRandNum(0, 14)] = mutatedBase; | |
return this.dna; | |
}, | |
compareDNA(pAequor) { | |
const dnaInCommon = this.dna.filter( | |
(base, idx) => pAequor.dna[idx] === base | |
); | |
console.log( | |
`specimen #${this.specimenNum} and specimen #${ | |
pAequor.specimenNum | |
} have ${dnaInCommon.length / this.dna.length} DNA in common` | |
); | |
}, | |
willLikelySurvive() { | |
const percentage = this.dna.reduce((acc, currVal) => { | |
if (currVal === "C" || currVal === "G") { | |
return acc + 1 / this.dna.length; | |
} | |
return acc; | |
}, 0); | |
if (percentage < 0.6) { | |
return false; | |
} | |
return true; | |
}, | |
}; | |
} | |
const species01 = pAequorFactory(1, mockUpStrand()); | |
console.log("dna of species01 before mutation: ", species01.dna); | |
species01.mutate(); | |
console.log("dna of species01 after mutation: ", species01.dna); | |
const species02 = pAequorFactory(2, mockUpStrand()); | |
species01.compareDNA(species02); | |
console.log(species01.willLikelySurvive()); | |
function createSpecimen(specimenLength, startNum) { | |
const survivableSpecimen = []; | |
let loopNum = specimenLength + startNum; | |
let i = startNum; | |
while (i < loopNum) { | |
let canSurvive = false; | |
while (!canSurvive) { | |
let specimen = pAequorFactory(i, mockUpStrand()); | |
if (specimen.willLikelySurvive()) { | |
canSurvive = true; | |
survivableSpecimen.push(specimen); | |
} | |
} | |
i++; | |
} | |
return survivableSpecimen; | |
} | |
const specimenArr = createSpecimen(30, 3); | |
console.log(specimenArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment