Last active
July 4, 2023 16:28
-
-
Save Platacoder/bd01a8c7f7e6446f391f094d97c6f55f to your computer and use it in GitHub Desktop.
This was a project I worked on while completing Codecademy's Full Stack Engineer career path. Context: You're part of a research team researching the P. aequor organism but it is difficult to study the organism in real life so you create a simulation of it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
let dnaStrand = mockUpStrand(); | |
//Creates an instance of P. aequor. Contains many functions. | |
const pAequorFactory = (specimenNum,dna) => { | |
return { | |
specimenNum: specimenNum, | |
dna: dna, | |
// Mutates a random base in the 15 base strand of DNA. Does not return the same base it mutated. | |
mutate(dna){ | |
var randomIndex = Math.floor(Math.random() * 15); | |
let mutatedBase = this.dna[randomIndex]; | |
switch (mutatedBase) { | |
case 'A': | |
this.dna.splice(randomIndex,1,['T', 'C', 'G'][Math.floor(Math.random() * 3)]); | |
break; | |
case 'T': | |
this.dna.splice(randomIndex,1,['A', 'C', 'G'][Math.floor(Math.random() * 3)]); | |
break; | |
case 'C': | |
this.dna.splice(randomIndex,1,['T', 'A', 'G'][Math.floor(Math.random() * 3)]); | |
break; | |
case 'G': | |
this.dna.splice(randomIndex,1,['T', 'C', 'A'][Math.floor(Math.random() * 3)]); | |
break; | |
}; | |
return this.dna | |
}, | |
// Compares DNA with another specimen's DNA and logs a message stating the percentage of DNA they share. | |
compareDNA(pAequor){ | |
let dnaCounter = 0; | |
for (let i = 0; i< this.dna.length; i++){ | |
if (pAequor.dna[i] === this.dna[i]){ | |
dnaCounter++; | |
} | |
} | |
let percentage = (dnaCounter/15)* 100; | |
console.log (`Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${percentage}% DNA in common.`); | |
}, | |
//Returns true if the object’s .dna array contains at least 60% 'C' or 'G' bases. Otherwise willLikelySurvive() returns false. | |
willLikelySurvive(){ | |
let gCounter = 0; | |
let cCounter = 0; | |
for (let i= 0; i< this.dna.length; i++){ | |
if (this.dna[i] === 'C'){ | |
cCounter++; | |
} | |
if (this.dna[i] === 'G'){ | |
gCounter++; | |
} | |
} | |
if (gCounter >= 9 || cCounter >= 9){ | |
return true; | |
} | |
else { | |
return false; | |
} | |
}, | |
}; | |
}; | |
// Holds all 30 surviving instances of P. aequor | |
let survivalArray = []; | |
// Creates 30 instances of P.aequor likely to survive (i.e. contain at least 60% of 'G' or 'C' in their DNA strands) | |
const createSurvivors = () => { | |
let id = 1; | |
do { | |
let newStrand = pAequorFactory(id,mockUpStrand()); | |
id++; | |
if (newStrand.willLikelySurvive() === true){ | |
survivalArray.push(newStrand); | |
} | |
} while (survivalArray.length < 30); | |
return survivalArray; | |
}; | |
console.log(createSurvivors()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I really liked your work, but you can improve this few lines:
if (this.dna[i] === 'C'){
cCounter++;
}
if (this.dna[i] === 'G'){
gCounter++;
}
by joining them and make the code cleaner and easier to read, like this:
if (this.dna[i] === 'C' || this.dna[i] === 'G' ){
gCounter++;
}