Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created November 16, 2021 14:28
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/13bd17eaa0aadcfeeeaae9b2606ce571 to your computer and use it in GitHub Desktop.
Save codecademydev/13bd17eaa0aadcfeeeaae9b2606ce571 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,
dna,
mutate(){
let i=Math.floor(Math.random()*15);
switch(this.dna[i]){
case 'A':{
this.dna[i]=['T','C','G'][Math.floor(Math.random()*3)];
}break;
case 'T':{
this.dna[i]=['A','C','G'][Math.floor(Math.random()*3)];
}break;
case 'C':{
this.dna[i]=['A','T','G'][Math.floor(Math.random()*3)];
}break;
case 'G':{
this.dna[i]=['A','T','C'][Math.floor(Math.random()*2)];
}break;
}
console.log(i);
return this.dna;
},
compareDNA(object){
let count=0;
for(i=0;i<this.dna.length;i++){
if(this.dna[i]===object.dna[i]){
count++;
}
}
commonPercentage=(count/this.dna.length)*100;
return commonPercentage.toFixed(2);
//console.log(`The specimen${this.specimenNum} and specimen${object.specimenNum} have ${commonPercentage.toFixed(2)}% of DNA in common`);
},
willLikelySurvive(){
let count=0;
for(i=0;i<this.dna.length;i++){
if(this.dna[i]==='C'||this.dna[i]==='G'){
count++;
}
}
survivePercentage=(count/this.dna.length)*100;
if(survivePercentage>=60){
return true;
}
else{
return false;
}
},
complementStrand(){
complementDNA=[];
for(i=0;i<this.dna.length;i++){
switch(this.dna[i]){
case 'A':{
complementDNA.push('T');
}break;
case 'T':{
complementDNA.push('A');
}break;
case 'C':{
complementDNA.push('G');
}break;
case 'G':{
complementDNA.push('C');
}break;
}
}
return complementDNA;
}
}
}
const survivingSpecimen=[];
let idCounter=1;
while(survivingSpecimen.length<30){
let newOrg=pAequorFactory(idCounter,mockUpStrand())
if(newOrg.willLikelySurvive()){
survivingSpecimen.push(newOrg);
}
idCounter++;
}
let highestPercentage = 0;
let currentPercentage=0;
let mostRelatedSpecimen1={};
let mostRelatedSpecimen2={};
for(let i of survivingSpecimen){
for(let j of survivingSpecimen){
if(j!==i){
currentPercentage=j.compareDNA(i);
if(currentPercentage>highestPercentage){
highestPercentage=currentPercentage;
mostRelatedSpecimen1=i;
mostRelatedSpecimen2=j;
}
}
}
}
console.log(mostRelatedSpecimen1.specimenNum+'\n'+mostRelatedSpecimen2.specimenNum+' '+highestPercentage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment