-
-
Save apassiondev/c896e6e03d68fca33caaa4de1638f8ab to your computer and use it in GitHub Desktop.
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
/** | |
* Codecademy mini challenge: Mysterious organism | |
* https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-ii/modules/fecp-challenge-project-mysterious-organism/projects/mysterious-organism | |
*/ | |
/** Provided helpers */ | |
// Returns a random DNA base | |
const returnRandBase = (_dnaBases = []) => { | |
if (!_dnaBases.length) | |
_dnaBases = ['A', 'T', 'C', 'G'] | |
return _dnaBases[indexRandomizer(_dnaBases.length)]; | |
}; | |
// 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; | |
}; | |
/** Challenge implementation */ | |
const log = console.log | |
const clear = console.clear | |
// To generate a random number with a given length | |
const indexRandomizer = len => Math.floor(Math.random() * len) | |
/** | |
* P.Aequor factory method | |
* | |
* @param {number} specimenNum | |
* @param {array} dna - generated by mockupStrand() | |
* @returns object | |
*/ | |
function PaequorFactory(specimenNum, dna) { | |
const pAequor = { | |
specimenNum, | |
dna, | |
mutate() { | |
// Pick a randomly selected target base => get its index | |
const targetIndex = indexRandomizer(this.dna.length) | |
// New selected bases must be different from the last base | |
// e.g: `A` is replaced by `T`, `G`, or `C` but NOT `A` | |
const possibleBases = this.dna.filter(db => db !== this.dna[targetIndex]) | |
// Assign the randomly selected to the given target base | |
this.dna[targetIndex] = possibleBases[indexRandomizer(possibleBases.length)] | |
}, | |
compareDNA(targetPaequor) { | |
// compute num of identical bases (in the same locations) | |
const sameness = this.dna.reduce((acc, cur, idx, arr) => { | |
if (arr[idx] === targetPaequor.dna[idx]) | |
return acc + 1 | |
else | |
return acc | |
}, 0) | |
// converted to percentage | |
const percentage = (sameness / this.dna.length * 100).toFixed(2) | |
// Output | |
log(`specimen #${this.specimenNum} and specimen #${targetPaequor.specimenNum} have ${percentage}% DNA in common.`) | |
}, | |
willLikelySurvive() { | |
const surviveCount = this.dna.filter(dna => ['C', 'G'].includes(dna)) | |
return (surviveCount.length / this.dna.length) * 100 >= 60 | |
}, | |
report() { | |
log(`---< Report specimen #${this.specimenNum} >---`) | |
console.table({ | |
id: this.specimenNum, | |
dna: this.dna.join(' '), | |
survival: this.willLikelySurvive() | |
}) | |
} | |
}; | |
return pAequor | |
} | |
const testables = [] | |
let counter = 0 | |
while (testables.length < 10) { | |
testables.push(PaequorFactory(counter + 1, mockUpStrand())) | |
// testables[counter].report() | |
counter += 1 | |
} | |
testables[2].report() | |
testables[5].report() | |
testables[2].compareDNA(testables[5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment