Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 7, 2021 22:58
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/be4555641ef5d8303823b1fba9ff66df to your computer and use it in GitHub Desktop.
Save codecademydev/be4555641ef5d8303823b1fba9ff66df 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 = (specimen, dna) =>{
return {
specimen: specimen,
dna: dna,
mutate(){
const mutBase = Math.floor(Math.random()*dna.length);
let changeBase = returnRandBase();
while(changeBase === dna[mutBase]){
console.log("Same base");
changeBase = returnRandBase();
};
this.dna[mutBase] = changeBase;
console.log(`Mutated strand: ${this.dna}`)
return this.dna ;
},
compareDNA(pAequorObj){
let count = 0;
//console.log(this.dna.length);
for(let i=0; i< this.dna.length; i++){
if(this.dna[i] === pAequorObj.dna[i]){
count+=1;
};
};
let result = ((count/this.dna.length)*100).toFixed(1);
return `Specimen ${this.specimen} and specimen ${pAequorObj.specimen} have ${result}% of their DNA in common.`;
},
willLikelySurvive(){
let counter = 0;
let countBase;
for(let k=0; k<this.dna.length; k++){
if(this.dna[k] === 'C' || this.dna[k]=== 'G'){
counter+=1;
//console.log(`This DNA has ${counter} bases of G and C.`)
};
countBase = ((counter/15)*100).toFixed(1);
};
console.log(`This DNA is made up of ${countBase}% of C and G bases.`);
if(countBase >= 60){
return true;
}else{
return false;
};
},
};
};
let test = pAequorFactory(1, mockUpStrand());
let test2 = pAequorFactory(2, mockUpStrand());
console.log(test.dna);
let testMutate = test.mutate();
console.log(testMutate);
console.log(test.compareDNA(test2));
console.log(test.willLikelySurvive());
let store = [];
let m = 0;
while(store.length < 30){
let instance = pAequorFactory(m, mockUpStrand());
if(instance.willLikelySurvive() === true ){
store.push(instance.dna);
m++;
};
console.log(store);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment