Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 13, 2021 04:51
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/541dcfe11d3276f26185d6ed7d21d959 to your computer and use it in GitHub Desktop.
Save codecademydev/541dcfe11d3276f26185d6ed7d21d959 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
}
//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