Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created October 7, 2020 06:46
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/44497e7b609ffc1d707080f17cd5ae16 to your computer and use it in GitHub Desktop.
Save codecademydev/44497e7b609ffc1d707080f17cd5ae16 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 = (number, dna) => {
return{
specimenNum: number,
dna,
mutate() {
const randomBase = Math.floor(Math.random() * 15 + 1);
const baseToMutate = dna[randomBase];
let mutatedBase;
do {
mutatedBase = returnRandBase(baseToMutate);
}
while(mutatedBase === baseToMutate);
dna[randomBase] = mutatedBase;
return dna;
},
compareDNA(otherSpecimen) {
let count = 0;
for(let i = 0; i < dna.length; i++){
dna[i] === otherSpecimen.dna[i] ? count++ : null;
}
let resemblance = count / dna.length * 100;
console.log(`Specimen ${this.specimenNum} and specimen ${otherSpecimen.specimenNum} have ${resemblance.toFixed(2)}% DNA in common.`)
return resemblance;//to be used in finding the most compatible specimens in the array of 30
},
willLikelySurvive() {
let count = 0;
for(let i = 0; i < dna.length; i++){
dna[i] === 'C' ? count++ : dna[i] === 'G' ? count++ : null;
}
let survivability = count / dna.length * 100;
return survivability >= 60 ? true : false;
},
complementStrand() {
let complementaryStrand = [];
for(let i = 0; i < dna.length; i++){
switch(dna[i]){
case 'A':
complementaryStrand.push('T');
break;
case 'T':
complementaryStrand.push('A');
break;
case 'C':
complementaryStrand.push('G');
break;
case 'G':
complementaryStrand.push('C');
break;
}
}
return complementaryStrand;
}
}
};
//create the array of thirty specimens with high survivability
const survivors = [];
while(survivors.length < 30) {
let specimen = pAequorFactory(survivors.length + 1, mockUpStrand());
if(specimen.willLikelySurvive) {
survivors.push(specimen);
}
}
console.log(survivors);
//find the two most related strands
let compatibility = 0;
let mostCompatible;
survivors.forEach(spec => {
for(let i = survivors.indexOf(spec) + 1; i < survivors.length;
i++){
if(spec.compareDNA(survivors[i]) > compatibility) {
compatibility = spec.compareDNA(survivors[i]);
mostCompatible = `These are the specimen numbers of the most compatible specimens: ${spec.specimenNum}, ${survivors[i].specimenNum}.`;
}
}
});
console.log(mostCompatible);
//calculate a complementary strand
const sp31 = pAequorFactory(31, mockUpStrand());
console.log(sp31.dna);
console.log(sp31.complementStrand());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment