Last active
January 20, 2021 17:31
-
-
Save AidaP1/0770307979e00d4e8d3c83decc0f7771 to your computer and use it in GitHub Desktop.
Codecademy Mysterious Organism Project
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 | |
} | |
const pAequorFactory = (num,arr) => { //factory function for new strand specimen | |
return { | |
specimenNum: num, | |
dna: arr, | |
mutate () { | |
//console.log(`old dna: ${this.dna}`) //checking log | |
let randomBaseIndex = Math.floor(Math.random() * this.dna.length) /// chooses a location to exchange the base | |
let newBase = returnRandBase() | |
while (newBase === this.dna[randomBaseIndex]) { // Rolls a new base until newBase !== current base at that position | |
newBase = returnRandBase() | |
} | |
this.dna[randomBaseIndex] = newBase; | |
//console.log(`New dna: ${this.dna}`) //checking log | |
return this.dna; | |
}, | |
compareDNA(pAequor) { // compare two strands and output to the console | |
let thisDNA = this.dna; | |
let compDNA = pAequor.dna; | |
let matchCount = 0 | |
for (i = 0; i < this.dna.length; i++) { //cycles through each array and log position + base matches on matchCount | |
if (thisDNA[i] === compDNA[i]) { | |
matchCount += 1; | |
}; | |
}; | |
let percMatch = Math.round(matchCount / this.dna.length * 100) //multiply by 100 to make 0.25 display as 25 in console log | |
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} are a ${percMatch}% DNA match.`) | |
return percMatch; | |
}, | |
compareDNAbulk(pAequor) { //as above, but does not log to console. Used for large arrays in findMostRelated() | |
let thisDNA = this.dna; | |
let compDNA = pAequor.dna; | |
let matchCount = 0 | |
for (i = 0; i < this.dna.length; i++) { | |
if (thisDNA[i] === compDNA[i]) { | |
matchCount += 1; | |
}; | |
}; | |
let percMatch = Math.round(matchCount / this.dna.length * 100) //multiply by 100 to make 0.25 display as 25 in console log | |
return percMatch; | |
}, | |
willLikelySurvive() { // looks for >= 60% of bases as either G or C | |
let countCG = 0; | |
this.dna.forEach(base => { | |
if (base === 'C' || base ==='G') { | |
countCG +=1; | |
} | |
}) | |
//console.log(countCG) // testing | |
//console.log(this.dna) // testing | |
return countCG / this.dna.length >= 0.6 | |
}, | |
complementStrand() { | |
return this.dna.map(base => { | |
switch (base) { | |
case 'A' : | |
return 'T'; | |
case 'T' : | |
return 'A'; | |
case 'C' : | |
return 'G'; | |
case 'G' : | |
return 'C'; | |
} | |
}) | |
} //close method | |
} // close object | |
} // close factory function | |
function generatepAequorArray(num) { // Generatess 'num' pAequor that .willLikelySurvive() = true | |
let pAequorArray = []; //result array | |
for (i = 0; pAequorArray.length < num; i++) { | |
let newpAequor = pAequorFactory(i,mockUpStrand()); // runs factory until there are 30 items in the result array | |
if (newpAequor.willLikelySurvive() === true) { | |
pAequorArray.push(newpAequor) | |
} | |
} | |
return pAequorArray; | |
} | |
function findMostRelated (array) { // champion/challenger function to find the most related specimens | |
let winningScore = 0; | |
let winner1; | |
let winner2; | |
for (let i = 0; i < array.length; i++) { | |
for ( let j = i; j < array.length; j++) // j = i to halve the number of loops. i = 0, j = 5 is the same as i = 5, j = 0 | |
if (i !== j) { // Do not check specimen against itself | |
let currentScore = array[i].compareDNAbulk(array[j]); | |
if (currentScore > winningScore) { // Challenger becomes the champion if they are a closer match | |
winningScore = currentScore; | |
winner1 = array[i].specimenNum; | |
winner2 = array[j].specimenNum; | |
} | |
} | |
} | |
let resultArray = [winner1,winner2,winningScore] // stored together for easy return | |
//console.log(`The most related specimens are specimen #${winner1} and specimen #${winner2}, with a ${winningScore}% match.`) | |
return resultArray | |
} | |
function multiArray (loops) { //test by running finding the closest match in 30 random 'will survive' samples, repaeated 1000 times. Returns the highest and lowest match across the 1000 runs | |
let highScore = 0; | |
let lowScore = 100 | |
for (let i = 0; i < loops; i++) { | |
let pAequorArray = generatepAequorArray(30); | |
let currentArray = findMostRelated(pAequorArray); | |
highScore = Math.max(highScore,currentArray[2]) | |
lowScore = Math.min(lowScore,currentArray[2]) | |
} | |
return results = { | |
'high score' : highScore, | |
'low score' : lowScore | |
} | |
} | |
// All code called below this line | |
console.log(multiArray(1000)) | |
/* | |
const pAequor1 = pAequorFactory(1,mockUpStrand()) | |
const arr1 = pAequor1.complementStrand() | |
const pAequor2 = pAequorFactory(2,arr1) | |
console.log(pAequor1.willLikelySurvive()) // ran the code a few times until this read 'true' | |
pAequor1.compareDNA(pAequor2) //0%! | |
console.log(pAequor1) | |
console.log(pAequor2) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment