Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 12, 2021 13:35
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 codecademydev/3606026145a95d159c79fad4ff975984 to your computer and use it in GitHub Desktop.
Save codecademydev/3606026145a95d159c79fad4ff975984 to your computer and use it in GitHub Desktop.
Codecademy export
// 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 = (num, array) => {
return {
specimenNum: num,
dna: array,
mutate() {
let randomNum = Math.floor(Math.random() * this.dna.length - 1);
let originalVal = this.dna[randomNum];
let newValue = returnRandBase();
for (let i = 0; newValue !== originalVal; i++) {
newValue = returnRandBase();
};
this.dna[randomValue] = newValue;
return this.dna;
},
compareDNA (pAequor) {
let arr1 = this.dna;
let arr2 = pAequor.dna;
let comparedArr = [];
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] === arr2[i]) {
comparedArr.push(arr1[i]);
}
};
let percentage = Math.floor((100 * comparedArr.length) / arr1.length);
console.log(`specimen #${this.specimenNum} and specimen #${pAequor.specimenNum} have ${percentage}% DNA in common`);
},
willLikelySurvive () {
let csAndGs = [];
for (let i = 0; i < this.dna.length; i++) {
if (this.dna[i] === 'C' || this.dna[i] === 'G') {
csAndGs.push(this.dna[i]);
}
};
let survivePercentage = (100 * csAndGs.length) / this.dna.length;
if (survivePercentage >= 60) {
return true;
} else {
return false;
}
},
}
};
const generator = (array) => {
for (let i = 1; i <= 30; i++) {
array.push(pAequorFactory(i, mockUpStrand()));
}
};
const thirtyInstances = [];
generator(thirtyInstances);
console.log(thirtyInstances[26]);
console.log(thirtyInstances[26].willLikelySurvive());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment