Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created Sep 11, 2022
Embed
What would you like to do?
Codecademy export
// 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 = (specimenNum, dna) => {
return ({
specimenNum: specimenNum,
dna: dna,
mutate(){
const randBase = this.dna(Math.floor(Math.random() * this.dna.length))
let newBase = returnRandBase()
while (randBase != newBase){
newBase = returnRandBase()
}
this.dna[i] = newBase
return this.dna
},
compareDNA(pAequorObj){
let identicalBases = 0
for (base in pAequorObj.dna){
if (pAequorObj.dna[base] == this.dna[base]){
identicalBases++
}
}
const percentEqual = (identicalBases/this.dna.length)*100
return `specimen ${this.specimenNum} and ${pAequorObj.specimenNum} have ${percentEqual}% DNA in common`
},
willLikelySurvive(dnaArray){
let CGBases = 0
for (base in dnaArray){
if (dnaArray[base] == 'C' || dnaArray[base] == 'G'){
CGBases++
}
}
return (CGBases/dnaArray.length)*100 >= 60
},
complementStrand(dnaArray){
let complement = []
for (base in dnaArray){
switch(dna[base]){
case 'A':
complement.push('T')
break;
case 'T':
complement.push('A')
break;
case 'C':
complement.push('G')
break;
case 'G':
complement.push('C')
break;
}
}
return complement
}
})
}
// create 30 instances that can survive in their environment
const thirtySamples = () => {
let pAequorArray = []
let createRandDNA = []
while (pAequorArray.length != 30){
createRandDNA = mockUpStrand()
let surviving = pAequorFactory(pAequorArray.length, createRandDNA).willLikelySurvive(createRandDNA)
if (surviving == true){
pAequorArray.push(pAequorFactory(pAequorArray.length, createRandDNA))
}
}
return pAequorArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment