Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created April 4, 2020 11:31
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/63d70449ca613bb1cdcc65df2ed2611b to your computer and use it in GitHub Desktop.
Save codecademydev/63d70449ca613bb1cdcc65df2ed2611b 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;
};
// dnaBases, array of 15 DNA bases
const pAequorFactory = (number, dnaBases) => {
const pAequor = {
specimenNum: number,
dna: dnaBases,
mutate() {
const randomIndex = Math.floor(Math.random() * dnaBases.length - 1);
const dnaToBeReplaced = this.dna[randomIndex];
do {
this.dna[randomIndex] = returnRandBase();
} while (this.dna[randomIndex] !== dnaToBeReplaced)
},
compareDNA(otherDna) {
let counter = 0;
for (let i = 0; i < otherDna.dna.length; i++) {
if (this.dna[i] === otherDna.dna[i]) {
counter++;
}
}
console.log(`specimen #${this.specimenNum} and specimen #${otherDna.specimenNum}
have ${ (+counter / +this.dna.length) * 100}% DNA in common. `);
return +counter / +this.dna.length * 100;
},
willLikelySurvive() {
let counter = 0;
counter = this.dna.reduce((acc, curr) => curr === 'C' || curr === 'G' ? ++acc : acc, 0);
return counter / this.dna.length >= 0.6;
},
complementStrand() {
let dnaMap = new Map([['A', 'T'], ['T', 'A'], ['C', 'G'], ['G', 'C']]);
return this.dna.map(dna => dnaMap.get(dna));
}
};
return pAequor;
};
const stuff = pAequorFactory(0, mockUpStrand());
const createStuff = function (index, num, arr) {
if (index === num) {
return arr;
}
const stuff = pAequorFactory(index, mockUpStrand());
if (stuff.willLikelySurvive()) {
arr.push(stuff);
index++;
return createStuff(index, num, arr);
}
return createStuff(index, num, arr);
};
const samples = createStuff(0, 30, []);
// input: array of dna objects
// output: the dna pair with the most similarity
// - take out the dna
// - compare the dna with the next one
// - check if result is higher than current result
// - if make max the two objects
function findMaxDNAPair(currIndex, arr, max, dnaPair) {
//base case: if currIndex == array.length
if (currIndex === arr.length) {
return [dnaPair, max];
} else {
for (let i = currIndex + 1; i < arr.length; i++) {
const currDna = arr[currIndex];
const nexDna = arr[i];
const similarity = currDna.compareDNA(nexDna);
if (similarity > max) {
max = similarity;
dnaPair = [currDna, nexDna];
}
}
}
return findMaxDNAPair(currIndex + 1, arr, max, dnaPair);
}
console.log(findMaxDNAPair(0, samples, 0, []));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment