-
-
Save CodingCyrus/163e43d5c94d60c01abee328e0c4a1cd to your computer and use it in GitHub Desktop.
Challenge Project: Mysterious Organism - CodingCyrus
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 | |
} | |
// Factory function that returns an object containing the properites specimenNum and dna in addition to mutate() and compareDNA() methods | |
const pAequorFactory = (specimenNum, dna) => { | |
return { | |
specimenNum: specimenNum, | |
dna: dna, | |
mutate() { | |
const dnaBases = ['A', 'T', 'C', 'G'] | |
let bases = Math.floor(Math.random() * 4) | |
let base = Math.floor(Math.random() * 15) | |
if (dna[base] != dnaBases[bases]) { | |
dna[base] = dnaBases[bases] | |
return dna; | |
} else { | |
let index = dnaBases.indexOf(dna[base]); | |
dnaBases.splice(index, 1); | |
dna[base] = dnaBases[Math.floor(Math.random() * 3)]; | |
return dna; | |
} | |
}, | |
compareDNA(pAequor) { | |
let identical = 0; | |
for (let i = 0; i < dna.length; i++) { | |
if(dna[i] === pAequor.dna[i]) { | |
identical++; | |
} else { | |
continue; | |
} | |
} | |
const percentage = (identical/15) * 100; | |
console.log(`specimen #1 and speciment #2 have ${percentage.toFixed(2)}% DNA in common.`); | |
}, | |
willLikelySurvive() { | |
let count = 0; | |
for (let i = 0; i < dna.length; i++) { | |
if (dna[i] == 'C' || dna[i] == 'G') { | |
count++; | |
} | |
} | |
if((count/15)*100 >= 60) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
} | |
//Array of 30 pAequor instances that can survive in their natural environment | |
let samples = []; | |
while (samples.length < 30){ | |
let obj = pAequorFactory(1, mockUpStrand()); | |
if (obj.willLikelySurvive()) { | |
samples.push(obj); | |
} else { | |
continue; | |
} | |
} | |
//Test Cases | |
// let obj = pAequorFactory(1, mockUpStrand()) | |
// console.log(obj.dna); | |
// // console.log(obj.mutate()) | |
// let obj2 =pAequorFactory(1, mockUpStrand()) | |
// console.log(obj2.dna); | |
// obj2.compareDNA(obj); | |
// console.log(obj2.willLikelySurvive()); | |
// console.log(samples); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment