Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created September 7, 2020 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/1fa5b9b5b0267efb276ad44808d774a5 to your computer and use it in GitHub Desktop.
Save codecademydev/1fa5b9b5b0267efb276ad44808d774a5 to your computer and use it in GitHub Desktop.
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=(number,dna)=>{
return {
specimenNum : number,
dna:dna,
//The mutation function:
mutate(){
let randPosition=[Math.floor(Math.random() * 15)];
let RandDNA=returnRandBase();
while(this.dna[randPosition]==RandDNA){
RandDN=returnRandBase();
}
console.log('The strand from position: '+ rando +' is '+ this.dna[rando]+' and has been replaced by '+ RandDNA);
this.dna[rando]=RandDNA;
},
//The comparison: checks to see how many items are identical both in the same position
compareDNA(pAequor){
let ret = 0;
this.dna.forEach((strand,i) =>{
if(strand === pAequor.dna[i])
ret++;
});
ret = Math.floor((ret / this.dna.length) * 100);
console.log(`specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${ret}% DNA in common`)
},
// Checks to see how many times C or G appear in the array
willLikelySurvive(){
let CorG=[0,0];
this.dna.forEach(x=>{
if(x=='C')
CorG[0]++;
else if(x=='G')
CorG[1]++;
});
if(CorG[0] >=9 || CorG[1] >=9)
return true;
return false;
},
// replaces every occurence of 'A' with 'T', 'T' with 'A', 'C' with 'G' and 'G' with 'C'
complementStrand(){
return this.dna.map(x=>{
if(x=='A')
return 'T';
if(x=='T')
return 'A';
if(x=='C')
return 'G';
if(x=='G')
return 'C';
return x;
});
}
}
}
// Creates an array of arrays with only DNA that can survive
function canSurvive (pAequor){
let i=0;
while(i< pAequor.length){
let temp= pAequorFactory(i,mockUpStrand());
if(temp.willLikelySurvive()){
pAequor[i]=temp;
i++;
}
};
return pAequor;
};
//let SomeStrand=pAequorFactory(1,mockUpStrand());
//console.log(SomeStrand.dna);
//console.log(SomeStrand.willLikelySurvive());
const request =canSurvive(new Array(30));
// Show all the DNA :
request.forEach(x=> console.log(`specimen ${x.specimenNum} is: ${x.dna} and has a ${x.willLikelySurvive()} chance of survival.`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment