-
-
Save codecademydev/a189bb2dec070fb6163bcdbcb44a4e18 to your computer and use it in GitHub Desktop.
Codecademy export
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, array) => { | |
return { | |
_specimenNum: num, | |
_dna: array, | |
mutate(){ | |
let randomBaseIndex = (Math.floor(Math.random() * array.length)); | |
console.log(randomBaseIndex); | |
let newRandBase = returnRandBase(); | |
console.log(newRandBase); | |
while (newRandBase === this._dna[randomBaseIndex]){ | |
newRandBase = returnRandBase(); | |
console.log(newRandBase) | |
} | |
this._dna[randomBaseIndex] = newRandBase; | |
return this._dna | |
}, | |
compareDNA(passedPAequor){ | |
let firstArray = this._dna; | |
let secondArray = passedPAequor._dna; | |
let dnaInCommon = [] | |
for (let i = 0; i < firstArray.length;i++){ | |
if(firstArray[i]===secondArray[i]){ | |
dnaInCommon.push(firstArray[i]) | |
} | |
} | |
console.log(dnaInCommon) | |
let percentageInCommon = dnaInCommon.length / 15 * 100; | |
return `specimen #${this._specimenNum} and specimen #${passedPAequor._specimenNum} have ${percentageInCommon}% DNA in common`; | |
}, | |
willLikelySurvive(){ | |
let cgCompare = this._dna.filter(elem => elem === 'C' || elem === 'G'); | |
let cgPercentage = cgCompare.length / 15 * 100 | |
if (cgPercentage >= 60){ | |
return true | |
} else { | |
return false | |
} | |
} | |
} | |
}; | |
// creating 30 instances | |
let specimens = []; | |
for (let i = 1; i <= 30; i++) { | |
do specimens[i] = pAequorFactory(i, mockUpStrand(returnRandBase)); | |
while (specimens[i].willLikelySurvive === false); | |
} | |
newSpeciesK = pAequorFactory(1, mockUpStrand(returnRandBase)) | |
newSpeciesL = pAequorFactory(2, mockUpStrand(returnRandBase)) | |
console.log(newSpeciesK) | |
console.log(newSpeciesL) | |
console.log(newSpeciesK.compareDNA(newSpeciesL)) | |
// will likelySurvive | |
console.log(newSpeciesK.willLikelySurvive()) | |
// creating 30 instances | |
console.log(specimens) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment