-
-
Save codecademydev/2fbf05196d140e6718e668b80685ac06 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, dnaArray) => { | |
return { | |
specimenNum: number, | |
dna: dnaArray, | |
mutate() { | |
for (let base in this.dna) { | |
let mutatedBase = returnRandBase(); | |
while (dnaArray[base] === mutatedBase) mutatedBase = returnRandBase(); | |
dnaArray[base] = mutatedBase; | |
} | |
return this.dna; | |
}, | |
compareDNA(pAequor) { | |
let dnaMatch = 0; | |
for (let i = 0; i < this.dna.length; i++){ | |
if (this.dna[i] === pAequor.dna[i]) dnaMatch++; | |
} | |
const matchPercent = (dnaMatch / this.dna.length) * 100; | |
console.log(`Specimen #${pAequor.specimenNum} and Specimen #${this.specimenNum} have ${matchPercent}% DNA in common.`); | |
}, | |
willLikelySurvive() { | |
let countCG = 0; | |
for (let i = 0; i < this.dna.length; i++) { | |
if (this.dna[i] === "C" || this.dna[i] === "G") countCG++; | |
} | |
const surviveRate = Math.ceil(this.dna.length * 0.60); | |
if (countCG >= surviveRate ) return true; | |
else return false | |
} | |
} | |
}; | |
const instances = []; | |
for (let i = 0; i < 30; i++) { | |
let sample = pAequorFactory(i, mockUpStrand()); | |
if (sample.willLikelySurvive()) instances.push(sample); | |
} | |
console.log(instances); | |
const test = pAequorFactory(4, ['C', 'A', 'C', 'T', 'G', 'T']); | |
const test1 = pAequorFactory(6, ['A', 'T', 'G', 'G']); | |
test.compareDNA(test1); | |
console.log(test.willLikelySurvive()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment