-
-
Save codecademydev/729f6e92b41ffb7b7a0070174e508eb4 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 = (pAequorNumber, bases) => { | |
let pAequor = { | |
specimenNum: pAequorNumber, | |
dna: bases, | |
mutate() { | |
const randBase = Math.floor(Math.random() * 15); | |
const currentBase = this.dna[randBase]; | |
let newBase = ''; | |
const dnaBases = ['A', 'T', 'C', 'G']; | |
do { | |
newBase = dnaBases[Math.floor(Math.random() * 4)]; | |
} while(newBase === currentBase); | |
this.dna[randBase] = newBase; | |
}, | |
compareDNA(pAequor) { | |
let commonCount = 0; | |
for (let i = 0; i < this.dna.length; i++) { | |
if (this.dna[i] === pAequor.dna[i]) { | |
commonCount++; | |
} | |
} | |
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${Math.floor(commonCount/this.dna.length*100)}% DNA in common`); | |
}, | |
willLikelySurvive() { | |
let survivalCount = 0; | |
const survivalBar = 0.6; | |
for (let i = 0; i < this.dna.length; i++) { | |
if (this.dna[i] === 'C' || this.dna[i] === 'G') { | |
survivalCount++; | |
} | |
} | |
return survivalCount/this.dna.length >= survivalBar; | |
} | |
}; | |
return pAequor; | |
} | |
const create30InstancesOfPAequor = () => { | |
let pAequorArray = []; | |
for (let i = 0; i < 30; i++) { | |
pAequorArray.push(pAequorFactory(i, mockUpStrand())); | |
} | |
return pAequorArray; | |
} | |
let pAequorArr = create30InstancesOfPAequor(); | |
console.log(pAequorArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment