Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 14, 2022 21:31
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/b08ea6af46dbda5bbe2bd36d70ce32c3 to your computer and use it in GitHub Desktop.
Save codecademydev/b08ea6af46dbda5bbe2bd36d70ce32c3 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
}
//factory function that creates objects that represent P. aequor
const pAequorFactory = (specimenNum, dna) => {
return {
specimenNum,
dna,
mutate() { // function that simulates P. aequor‘s high rate of mutation
randomBaseIndex = Math.floor(Math.random() * this.dna.length)
let newBase = returnRandBase();
while (this.dna[randomBaseIndex] === newBase) {
newBase = returnRandBase();
}
this.dna[randomBaseIndex] = newBase;
return this.dna;
},
compareDNA(newObj) { //compare the DNA sequences of different instances of simluated P. aequor, showing specimen number and percentage of DNA shared.
let commonBases = 0;
let mostRelated = [];
let newStrand = newObj.dna;
for (let i= 0; i < this.dna.length; i++) {
if(this.dna[i] === newStrand[i]){
commonBases++;
}
}
let percentageShared = ( commonBases / this.dna.length) * 100;
console.log(`Specimen #${this.specimenNum} and Specimen #${newObj.specimenNum} have ${percentageShared.toFixed()}% DNA in common.`)
},
willLikelySurvive() { // checks if the current specimen has a likely chance of survival based on how many C or G bases are in it's DNA strand.
let survival = 0;
for (const base of this.dna) {
if (base === 'C' || base === 'G'){
survival++;
}
}
return survival / this.dna.length >= 0.6;
},
complementStrand() { // simulates the complementary strand of the current specimen's single DNA Strand.
let complement = [];
for (let i= 0; i < this.dna.length; i++){
if (this.dna[i] === 'A') {
complement.push('T');
} else if (this.dna[i] === 'T') {
complement.push('A');
} else if (this.dna[i] === 'C') {
complement.push('G');
} else {
complement.push('C')
}
}
return complement;
}
}
}
// Sample of 30 simulated specimens with a likely chance of survival.
const surviveSpec = [];
let specCount = 1;
while (surviveSpec.length < 30) {
let newSpec = pAequorFactory(specCount, mockUpStrand());
if (newSpec.willLikelySurvive()) {
surviveSpec.push(newSpec)
}
specCount++;
}
// testing section
const pAequor1 = pAequorFactory(1, mockUpStrand());
const pAequor2 = pAequorFactory(2, mockUpStrand());
console.log(pAequor1.dna)
console.log(pAequor1.complementStrand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment