-
-
Save codecademydev/541dcfe11d3276f26185d6ed7d21d959 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 | |
} | |
//object factory | |
const pAequorFactory = (specimenNum, dna) => { | |
console.log(dna) | |
return { | |
specimenNum : specimenNum, | |
dna : dna, | |
// mutate method to change a base | |
mutate() { | |
const dnaBases = ['A', 'T', 'C', 'G'] | |
let rand = this.dna[Math.floor(Math.random()*15)] ; | |
let base = dnaBases[Math.floor(Math.random() * 4)]; | |
if ( rand === base) { | |
return 'No mutation'; | |
}else { | |
dna.splice(dna.indexOf(rand),1, base); | |
return dna; | |
} | |
}, | |
// method to compare the mutated dna with another dna passed in | |
compareDNA(obj) { | |
let counter = 0; | |
for(let i = 0; i<dna.length; i++){ | |
if(dna[i]===obj.dna[i]){ | |
counter++; | |
console.log(counter); | |
} | |
} | |
const percentage = (counter/dna.length)*100; | |
console.log(`The correlation between specimen number ${this.specimenNum} and ${obj.specimenNum} showed a ${percentage}% match.`); | |
}, | |
//check the viability of samples with 60% C and G as the baseline | |
willLikelySurvive() { | |
let counter = 0; | |
for (let i =0; i<this.dna.length; i++){ | |
if (this.dna[i]==='C' || this.dna[i] === 'G'){ | |
counter++; | |
} | |
} | |
let percentage= (counter/this.dna.length)*100; | |
if(percentage>=60){ | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
} | |
} | |
let specimenNumber=1; | |
const ae = pAequorFactory(specimenNumber, mockUpStrand()) | |
//console.log(ae.mutate()); | |
//ae.compareDNA(pAequorFactory(2, mockUpStrand())); | |
//console.log(ae.willLikelySurvive()); | |
// 30 instances of viable samples | |
const sampleBatch = () => { | |
const batch = []; | |
while(batch.length <=30){ | |
pAequorFactory(specimenNumber, mockUpStrand()); | |
if(ae.willLikelySurvive()){ | |
batch.push(ae); | |
} | |
specimenNumber++; | |
} | |
return batch; | |
} | |
console.log(sampleBatch()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment