Last active
July 28, 2021 15:10
-
-
Save jalcoding8/87c469b9d6df64cc7f9a9b16b24dabb5 to your computer and use it in GitHub Desktop.
Javascript - Mysterious Organism(DNA) syntax 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 | |
}; | |
/*I often implement the use of console.log() so I can see and the progression/output of code lines. I also give myself lots of comments so when I revisit projects it reminds me of my logic used. | |
This helps me learn and recall*/ | |
const pAequorFactory = (specimenNum, dna) => { | |
return { | |
specimenNum: specimenNum, //original variables | |
dna: dna, //could have just put dna, (instead of dna: dna), | |
mutate() { | |
const randomIndex = Math.floor(Math.random() * 15); | |
console.log() | |
console.log("Below is the index in the original DNA strand that will be replaced.") | |
console.log(randomIndex) | |
const base = dna[randomIndex]; | |
console.log("Below is the value at the index.") | |
console.log(base) | |
let replacementBase = base; | |
console.log(`Will now mutate index ${randomIndex}/base ${base} of original DNA strand for specimen ${specimenNum}.`) | |
while (replacementBase === base) { | |
replacementBase = returnRandBase(); | |
} //use of helperfunction | |
//loop will generate the helper function until they dont match, and replace target value with one of the 3 other base values, selected randomly. | |
dna[randomIndex] = replacementBase; | |
//console.log(dna); | |
console.log(`The is the mutated strand of DNA of specimen ${specimenNum}, for comparison to the original above:`) | |
return dna; | |
//The mutate function shows the index in original dna strand, the value at index and the new modified strand to confirm the mutation worked and where, and which other base. | |
}, | |
compareDNA(pAequor) { | |
let similarCount = 0; | |
pAequor.dna.forEach((base, index) => { | |
if (base === dna[index]) { | |
similarCount++; } | |
} | |
) | |
console.log(`Specimen ${specimenNum} & Specimen ${pAequor.specimenNum} have ${Math.round((similarCount/15) * 100) }% DNA in common.`) | |
return Math.round((similarCount/15) * 100) +'%'; | |
}, | |
willLikelySurvive() { | |
let baseCCount = 0; | |
let baseGCount = 0; | |
for (let i = 0; i < dna.length; i++) { | |
if (dna[i] === 'C') { | |
baseCCount++; | |
} | |
if (dna[i] === 'G') { | |
baseGCount++; | |
} | |
} | |
let combinedCGCount = baseCCount + baseGCount; | |
console.log(`\n ${combinedCGCount} of the 15 bases of the Specimen ${specimenNum} DNA strand are either 'C' or 'G' (${baseCCount} 'C', + ${baseGCount} 'G')`) | |
console.log("The corresponding combined percentage is:") | |
let percentage = ""; | |
percentage = Math.round((combinedCGCount/15) * 100); //round to limit decimal points | |
console.log(percentage + "%") | |
if (percentage >= 60) { | |
console.log('Will survive is the assertion') | |
return true; | |
} console.log("Will NOT survive is the assertion") | |
return false; | |
}, | |
makeComplementaryStrand() { | |
let complementaryStrand = []; | |
for (let i = 0; i < dna.length; i++) { | |
if (dna[i] === 'A') { | |
complementaryStrand.push('T');} | |
else if (dna[i] === 'T') { | |
complementaryStrand.push('A');} | |
else if (dna[i] === 'G') { | |
complementaryStrand.push('C');} | |
else if (dna[i] === 'C') { | |
complementaryStrand.push('G');} | |
else { complementaryStrand.push(dna[i]) } | |
} | |
return`Generating the complementary DNA strand | |
(where base A will match to base T, and base C will match to base G, or T to A, or G to C) | |
Original DNA (array) strand: [${dna.join(', ')}]\nComplementary DNA (array) strand: [${complementaryStrand.join(', ')}]`; | |
} | |
} | |
}; | |
const makeWillSurviveBatch = () => { | |
const willSurviveArrayBatch = []; | |
let batchNum = 1; | |
while (willSurviveArrayBatch.length < 30) { | |
let makeSurvive = pAequorFactory(batchNum, mockUpStrand()); | |
if (makeSurvive.willLikelySurvive()) { | |
//I had 2 conditions not just true/false | |
//but also confirmed 60% or greater | |
willSurviveArrayBatch.push(makeSurvive); | |
} | |
batchNum++; | |
} | |
return willSurviveArrayBatch; | |
}; | |
//I need to refactor this function. It will return an array of DNA strands the are true for 'willLikelySurvive()' but also | |
//includes the DNA strands that are false. This is because I used console.log too much but I wanted to follow the logic of | |
//the factory function method so I understand why the output is as it is. I also have done some minor refactoring. | |
//varible to pass as argument when testing comparisons | |
const originalDna = "This is the original DNA strand of"; | |
//testing various calls of factory function | |
const firstDna = pAequorFactory(1, mockUpStrand()) | |
console.log(originalDna, firstDna) | |
console.log(firstDna.mutate()) | |
console.log(firstDna.willLikelySurvive()) | |
console.log() | |
const secondDna = pAequorFactory(2, mockUpStrand()) | |
console.log(originalDna, secondDna) | |
console.log(secondDna.mutate()) | |
console.log(firstDna.compareDNA(secondDna)) | |
console.log() | |
const thirdDna = pAequorFactory(3, mockUpStrand()) | |
console.log(originalDna, thirdDna) | |
console.log(thirdDna.mutate()) | |
console.log() | |
console.log(firstDna.compareDNA(thirdDna)) | |
'\n' | |
console.log(secondDna.compareDNA(thirdDna)) | |
console.log() | |
const fourthDna = pAequorFactory(4, mockUpStrand()) | |
console.log(originalDna, fourthDna) | |
console.log(fourthDna.mutate()) | |
console.log(fourthDna.willLikelySurvive()) | |
console.log() | |
const fifthDna = pAequorFactory(5, mockUpStrand()); | |
console.log(originalDna, fifthDna) | |
console.log(fifthDna.mutate()) | |
console.log(firstDna.willLikelySurvive()) | |
console.log() | |
console.log(firstDna.makeComplementaryStrand()) | |
console.log(makeWillSurviveBatch() | |
//console.log(pAequorFactory.surviveBatch) | |
//Testing the willLikelySurvive() function with survive dna that is hard-coded. | |
/*console.log(firstDna.willLikelySurvive(['C', 'C', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'C', 'C', 'G', 'C', 'C', 'C'])); | |
console.log() | |
console.log(secondDna.willLikelySurvive(['G', 'C', 'C', 'G', 'A', 'T', 'A', 'A', 'A', 'C', 'C', 'G', 'G', 'G', 'G'])); | |
console.log() | |
console.log(thirdDna.willLikelySurvive(['A', 'A', 'A', 'G', 'A', 'G', 'C', 'G', 'G', 'C', 'T', 'G', 'T', 'T', 'T'])); | |
console.log() | |
let fourthDna = pAequorFactory(4, mockUpStrand()) | |
console.log('Strand 4', fourthDna); | |
console.log(fourthDna.willLikelySurvive(fourthDna)) | |
console.log() | |
/*checking the helper functions output at start of project | |
console.log(returnRandBase()) | |
console.log(mockUpStrand())*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment