Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created Sep 26, 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 strand 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,
dna,
mutate(){
let randIndex=Math.floor(Math.random()* this.dna.length)
const newDnaBase= returnRandBase();
while(this.dna[randIndex]===newDnaBase){
newDnaBase=returnRandBase();
}
this.dna[randIndex]=newDnaBase
return this.dna;
},
compareDNA(pAequor){
let similarities= this.dna
.reduce((ac,current,idx,DnaArray)=>{
if(DnaArray[idx]===pAequor.dna[idx]){
return ac+1;
}else{
return ac;
}
},0);
let relationShip=((similarities * this.dna.length)/100).toFixed(2);
console.log(`specimen# ${this.specimenNum} and specimen# ${pAequor.specimenNum} have ${similarities} DNA in common`);
},
willLikelySurvive(){
let newgno= this.dna.filter((p)=>(p==='C' || p=='G'));
return newgno.length / this.dna.length >= 0.6;
},
complementStrand(){
const objectBase={'A':'T','T':'A','C':'G','G':'C'};
let compl=this.dna.map((base)=>{
return objectBase[base];
});
return compl;
}
}
}
const surviviSpacimen=[];
for(var idSpacimen=0; idSpacimen < 30; idSpacimen++){
let newOrg=pAequorFactory(idSpacimen, mockUpStrand());
if(newOrg.willLikelySurvive(newOrg)){
surviviSpacimen.push(newOrg);
}
}
console.log(surviviSpacimen);
//complement strand
let complementStrend = pAequorFactory(idSpacimen, mockUpStrand());
console.log(complementStrend.dna);
console.log(complementStrend.complementStrand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment