Created
July 7, 2022 14:32
-
-
Save sthursby16/b7e0c135a812e00f911e126c673a7426 to your computer and use it in GitHub Desktop.
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 strand of DNA containing 15 bases | |
const mockUpStrand = () => { | |
const newStrand = [] | |
for (let i = 0; i < 15; i++) { | |
newStrand.push(returnRandBase()) | |
} | |
return newStrand | |
} | |
const pAequorFactory = (num, arr) => { | |
return { | |
specimenNum: num, | |
dna: arr, | |
mutate() { | |
const randNum = Math.floor(Math.random() * 15); | |
const dnaBases = ['A', 'T', 'C', 'G'] | |
const randBase = returnRandBase(); | |
if (arr[randNum] !== randBase) { | |
arr[randNum] = randBase; | |
} else { | |
arr[randNum] = dnaBases[Math.floor(Math.random() * 4) - 1]; | |
} | |
const newArr = arr.splice(randNum, 1, arr[randNum]); | |
return newArr; | |
this.dna = newArr; | |
}, | |
compareDNA(pAequor) { | |
let numSame = 0; | |
for (let i=0; i<arr.length; i++) { | |
if (this.dna[i] === pAequor.dna[i]) { | |
numSame++; | |
} else { | |
numSame = numSame; | |
} | |
} | |
const percent = (numSame / 15) * 100; | |
return `specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${percent.toFixed(2)}% DNA in common.`; | |
}, | |
willLikelySurvive() { | |
let corg = 0; | |
for (let i=0; i<arr.length; i++) { | |
if (this.dna[i] === 'C') { | |
corg++; | |
} else if (this.dna[i] === 'G') { | |
corg++; | |
} | |
} | |
const corgPerc = (corg / 15) * 100; | |
if (corgPerc >= 60) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
} | |
let aequorSet = []; | |
let setDone = false; | |
for (let i=1; setDone === false; i++) { | |
let aequorObj = pAequorFactory(i, mockUpStrand()); | |
if (aequorObj.willLikelySurvive() === true) { | |
aequorSet.push(aequorObj); | |
} | |
if (aequorSet.length === 30) { | |
setDone = true; | |
} else { | |
setDone = false; | |
} | |
} | |
console.log(aequorSet); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment