Created
February 1, 2022 22:18
-
-
Save agent47nh/52c7ad4ce95ee2927a76aa1d6068dfef to your computer and use it in GitHub Desktop.
[Solution] Challenge Project by CodeAcademy on Back-End Engineer Career Path
This file contains hidden or 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 = (specimenNum, dna) => { | |
return { | |
_dna: dna, | |
_specimenNum: specimenNum, | |
get dna() { | |
return this._dna; | |
}, | |
set dna(newDna) { | |
this._dna = newDna; | |
}, | |
get specimenNum() { | |
return this._specimenNum; | |
}, | |
set specimenNum(newSpecimenNum) { | |
this._specimenNum = newSpecimenNum; | |
}, | |
mutate() { | |
const randIndex = Math.floor(Math.random() * this._dna.length); | |
this._dna[randIndex] = () => { | |
const currentBase = this._dna[randIndex]; | |
let selectBase = true; | |
do { | |
let generatedBase = returnRandBase(); | |
if (generatedBase !== currentBase) { | |
selectBase = false; | |
this._dna[randIndex] = generatedBase; | |
} | |
} while (selectBase); | |
return this._dna[randIndex]; | |
}; | |
}, | |
compareDNA(pAequor) { | |
let dnaMatch = 0; | |
for (let i = 0; i < this._dna.length; i++) { | |
if (this._dna[i] === pAequor.dna[i]) { | |
dnaMatch++; | |
} | |
} | |
console.log( | |
`Specimen #${this._specimenNum} and specimen #${pAequor.specimenNum} have ${dnaMatch / this._dna.length}% DNA in common.` | |
); | |
// return dnaMatch / this._dna.length; | |
}, | |
willLikelySurvive() { | |
let baseCount = 0; | |
this._dna.forEach((base) => { | |
if (base === 'C' || base === 'G') { | |
baseCount++; | |
} | |
}); | |
if (baseCount >= 9) { | |
return true; | |
} | |
return false; | |
}, | |
complementStrand() { | |
let complementStrandArray = []; | |
this._dna.forEach((base) => { | |
if (base === 'A') { | |
complementStrandArray.push('T'); | |
} else if (base === 'T') { | |
complementStrandArray.push('A'); | |
} else if (base === 'C') { | |
complementStrandArray.push('G'); | |
} else if (base === 'G') { | |
complementStrandArray.push('C'); | |
} | |
}); | |
return complementStrandArray; | |
}, | |
}; | |
}; | |
let specimenCount = 30; | |
const pAequorArray = []; | |
for(let i = 0; i < specimenCount; i++) { | |
const newSpecimen = pAequorFactory(i + 1, mockUpStrand()); | |
console.log(`Specimen #${newSpecimen.specimenNum} has DNA: ${newSpecimen.dna}`); | |
if(newSpecimen.willLikelySurvive()) console.log(`Specimen #${newSpecimen.specimenNum} will likely survive.`); | |
pAequorArray.push(newSpecimen); | |
console.log(`Specimen #${newSpecimen.specimenNum} - Complement strand: ${newSpecimen.complementStrand()}`); | |
if (i>0) { | |
newSpecimen.compareDNA(pAequorArray[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment