Skip to content

Instantly share code, notes, and snippets.

@franciss08
Created April 13, 2022 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franciss08/b98004d49a887d6690a6cc1ebf305717 to your computer and use it in GitHub Desktop.
Save franciss08/b98004d49a887d6690a6cc1ebf305717 to your computer and use it in GitHub Desktop.
mystery organism codecademy project
// 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;
};
function pAequorFactory (specimenNum, dna) {
return {
specimenNum: specimenNum,
dna: dna,
mutate: function() {
let mutatedDna = this.dna;
let basePosPicked = Math.floor(Math.random() * mutatedDna.length);
let currentBase = mutatedDna[basePosPicked];
let replacementBase;
do {
replacementBase = returnRandBase();
} while (replacementBase === currentBase);
mutatedDna[basePosPicked] = replacementBase;
},
compareDna: function(pAequor) {
let otherDna = pAequor.dna;
let currentDna = this.dna;
let counter = 0;
let matchCounter = 0;
for (let i = 0; i < otherDna.length; i++) {
if (otherDna[i] === currentDna[i]) {
matchCounter++;
}
counter++
}
console.log(`Specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${Math.round((100 * matchCounter) / counter)}% DNA in common.`)
},
willLikelySurvive: function () {
let counter = 0;
let matchCounter = 0;
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === 'C' || this.dna[i] === 'G') {
matchCounter++;
}
counter++
}
return a = (100 * matchCounter) / counter > 60 ? true : false;
},
}
}
//tests to check factory function
const instance1 = pAequorFactory(1, mockUpStrand());
const instance2 = pAequorFactory(2, mockUpStrand());
console.log(instance1);
instance1.mutate();
console.log(instance1);
console.log(instance1.willLikelySurvive());
instance1.compareDna(instance2);
instance2.compareDna(instance1);
//creating an array of 30 instances
const arrayOfInstances = [];
for (let n = 1; n < 31; n++) {
arrayOfInstances.push(pAequorFactory(n, mockUpStrand()));
}
console.log(arrayOfInstances);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment