Skip to content

Instantly share code, notes, and snippets.

@codecademydev

codecademydev/main.js Secret

Created Jan 5, 2021
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
}
function pAequorFactory(specimenNum, dna){
return{
specimenNum: specimenNum,
dna: dna,
mutate(){
console.log(this.dna)
let rand = Math.floor(Math.random()* this.dna.length);
console.log(rand)
console.log(this.dna[rand])
switch(this.dna[rand]){
case 'A': do{this.dna[rand]= returnRandBase()}while(this.dna[rand]== 'A')
break;
case 'C': do{this.dna[rand]= returnRandBase()}while(this.dna[rand]== 'C')
break;
case 'T': do{this.dna[rand]= returnRandBase()}while(this.dna[rand]== 'T')
break;
case 'G': do{this.dna[rand]= returnRandBase()}while(this.dna[rand]== 'G')
break;
default: `This is not possible.`
}
return this.dna
},
compareDNA(newDna){
let similarityIndex = 0
let similarity
let i=0
while(i<this.dna.length){
if(this.dna[i]===newDna.dna[i]){
similarityIndex+=1
}
i++
}
console.log(similarityIndex)
similarity = (similarityIndex/this.dna.length)*100
return `Specimen ${this.specimenNum} and specimen ${newDna.specimenNum} have ${similarity}% DNA in common`
},
willLikelySurvive(aDNA){
let survivalChance=0
for(let i=0; i<aDNA.length; i++){
if(aDNA[i]=='C' || aDNA[i]=='G'){
survivalChance+=1
}
}
survivalChance=(survivalChance * 100)/this.dna.length
if(survivalChance>=60){
return true
}else{
return false
}
}
}
}
//const works = pAequorFactory(1, mockUpStrand())
//console.log(works)
//console.log(works.mutate())
let check = pAequorFactory(404, mockUpStrand())
//console.log(hello.mutate())
//console.log(pAequorFactory())
//console.log(works.compareDNA(hello))
let studyArray=[]
let j=1
for(let i=1; i<=30; i++){
let k =mockUpStrand()
while(check.willLikelySurvive(k)==false){
k = mockUpStrand()
}
studyArray.push(new pAequorFactory(j, k))
j+=1
}
console.log(studyArray)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment