Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 18, 2020 12: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 codecademydev/f8db5ea0b8f27f94a920b05ae2cb209b to your computer and use it in GitHub Desktop.
Save codecademydev/f8db5ea0b8f27f94a920b05ae2cb209b 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 = (specimenNum, dna) =>{
return{
specimenNum: specimenNum,
dna: dna,
mutate(){
const randomBase = Math.floor(Math.random() * 15);
switch(this.dna[randomBase]){
case 'A':
while(this.dna[randomBase] === 'A'){
this.dna[randomBase] = returnRandBase();
}
break;
case 'C':
while(this.dna[randomBase] === 'C'){
this.dna[randomBase] = returnRandBase();
}
break;
case 'T':
while(this.dna[randomBase] === 'T'){
this.dna[randomBase] = returnRandBase();
}
break;
case 'G':
while(this.dna[randomBase] === 'G'){
this.dna[randomBase] = returnRandBase();
}
break;
}
return this.dna;
},
compareDNA(pAequor){
let cont = 0;
for(let i = 0; i < 14; i++){
if(this.dna[i] === pAequor.dna[i]){
cont++;
}
}
const porcentage = (cont/15*100).toFixed(2);
//console.log(`specimen #1 and specimen#2 have ${porcentage}% DNA in common`);
return porcentage;
},
willLikelySurvive(){
let contC = 0;
let contG = 0;
for(let i = 0; i < 14; i++){
if(this.dna[i] === 'C'){
contC++;
continue;
}
if(this.dna[i] === 'G'){
contG++;
}
}
const porcentageC = (contC/15*100);
const porcentageG = (contG/15*100);
if(porcentageC >= 60 || porcentageG >= 60){
return true;
}
return false;
},
complementStrand(){
for(let i = 0; i > 15; i++){
switch(this.dna[i]){
case 'A':
this.dna[i] = 'T';
break;
case 'T':
this.dna[i] = 'A';
break;
case 'C':
this.dna[i] = 'G';
break;
case 'G':
this.dna[i] = 'C';
break;
}
}
}
}
}
survivors = [];
let i = 1;
while(i < 31){
const bug = pAequorFactory(i, mockUpStrand());
if(bug.willLikelySurvive()){
survivors.push(bug);
i++;
}
}
let mayor = 0;
let specimen1;
let specimen2;
for(let j = 0; j < 30; j++){
for(let k = 0; k < 30; k++){
if(j === k){
continue;
}
if(survivors[j].compareDNA(survivors[k]) > mayor){
mayor = survivors[j].compareDNA(survivors[k]);
specimen1 = survivors[j];
specimen2 = survivors[k];
}
}
}
console.log(`Specimens ${specimen1.specimenNum} and ${specimen2.specimenNum} are the most related instances of P. Aequor with a similarity of ${mayor}%.`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment