Advertisement
makispaiktis

Codecademy - Open Exercise (pAequor DNA Research)

Dec 23rd, 2019 (edited)
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Returns a random DNA base
  2. const returnRandBase = () => {
  3.   const dnaBases = ['A', 'T', 'C', 'G']
  4.   return dnaBases[Math.floor(Math.random() * 4)]
  5. }
  6.  
  7. // Returns a random single stand of DNA containing 15 bases
  8. const mockUpStrand = () => {
  9.   const newStrand = []
  10.   for (let i = 0; i < 15; i++) {
  11.     newStrand.push(returnRandBase())
  12.   }
  13.   return newStrand
  14. }
  15.  
  16. // Create a factory function for pAequor
  17. function pAequorFactory(num, DNABasesArray){
  18.   return {
  19.     specimenNum: num,
  20.     dna: DNABasesArray,
  21.     mutate(){
  22.       let randomIndex = Math.floor(Math.random() * 15);
  23.       let base = this.dna[randomIndex];
  24.       let newBase = returnRandBase();
  25.       while(base === newBase){
  26.         newBase = returnRandBase();
  27.       }
  28.       // I will copy the dna array to new array
  29.       let newDNA = this.dna.slice();
  30.       newDNA[randomIndex] = newBase;
  31.       return newDNA;
  32.     },
  33.     compareDNA(otherPAequor){
  34.       let sum = 0;
  35.       for(let i=0; i<15; i++){
  36.         if(this.dna[i] === otherPAequor.dna[i]){
  37.           sum += 1;
  38.         }
  39.       }
  40.       console.log(`We compared the 2 DNA chains of pAequors with ids: ${this.specimenNum} and ${otherPAequor.specimenNum}.`);
  41.       let percentage = (Math.round(10000 * sum / 15)) / 10000;
  42.       console.log("We found out that these 2 pAequors samples are similar in the percentage of " + (100 * percentage) + "%.");
  43.     },
  44.     willLikelySurvive(){
  45.       let sum = 0;
  46.       for(let i=0; i<15; i++){
  47.         if(this.dna[i] === 'C' || this.dna[i] === 'G'){
  48.           sum += 1;
  49.         }
  50.       }
  51.       if(sum / 15 >= 0.6){
  52.         return true;
  53.       }
  54.       else{
  55.         return false;
  56.       }
  57.     },
  58.     complementaryStrand(){
  59.       let complement = [];
  60.       for(let i=0; i<15; i++){
  61.         if(this.dna[i] === 'A'){
  62.           complement.push('T');
  63.         }
  64.         else if(this.dna[i] === 'T'){
  65.           complement.push('A');
  66.         }
  67.         else if(this.dna[i] === 'G'){
  68.           complement.push('C');
  69.         }
  70.         else if(this.dna[i] === 'C'){
  71.           complement.push('G');
  72.         }
  73.       }
  74.       return complement;
  75.     }
  76.   };  // END OF RETURNED OBJECT
  77. }  // END OF FACTORY FUNCTION
  78.  
  79. let dnaBases = mockUpStrand();
  80. let pAequor = pAequorFactory(1, dnaBases);
  81. let dnaBases2 = mockUpStrand();
  82. let pAequor2 = pAequorFactory(2, dnaBases2);
  83. console.log("pAequors:");
  84. console.log(pAequor);
  85. console.log(pAequor2);
  86. console.log();
  87. console.log("DNA chains of the 2 pAequors and the 2 mutations of them:");
  88. console.log(dnaBases);
  89. console.log(pAequor.mutate());
  90. console.log();
  91. console.log(dnaBases2);
  92. console.log(pAequor2.mutate());
  93. console.log();
  94. console.log("Compare the 2 DNAs:");
  95. pAequor.compareDNA(pAequor2);
  96. console.log();
  97. console.log("pAequor will likely survive? " + pAequor.willLikelySurvive());
  98. console.log("pAequor2 will likely survive? " + pAequor2.willLikelySurvive());
  99.  
  100. // Now, I want to create 30 instances of pAequor object (using the pAequorFactory() function), that can survive in their natural environment
  101. pAequorArray = [];
  102. let counter = 0;
  103. while(pAequorArray.length < 30){
  104.   counter++;
  105.   let pAequorInstance = pAequorFactory(counter, mockUpStrand());
  106.   if(pAequorInstance.willLikelySurvive() === true){
  107.     pAequorArray.push(pAequorInstance);
  108.   }
  109. }
  110. //console.log();
  111. //console.log(pAequorArray);
  112.  
  113. // Now, the variable 'pAequorArray' includes the 30 specimens that survived
  114. console.log();
  115. console.log("Complementary DNA chains: ");
  116. console.log(pAequor.complementaryStrand());
  117. console.log(pAequor2.complementaryStrand());
  118.  
  119. // Last task is to scan the array of pAequor instances and find the 2 most related instances
  120. let maxPercentage = 0;
  121. let maxI = 0;
  122. let maxJ = 0;
  123. for(let i=0; i<pAequorArray.length; i++){
  124.   let instance1 = pAequorArray[i];
  125.   for(let j=0; j<pAequorArray.length; j++){
  126.     let instance2 = pAequorArray[j];
  127.     if(i != j){  // In order to compare the different instances
  128.       let sum = 0;
  129.       for(let k=0; k<instance1.dna.length; k++){
  130.         if(instance1.dna[k] === instance2.dna[k]){
  131.           sum += 1;
  132.         }
  133.       }
  134.       let percentage = sum / instance1.dna.length;
  135.       if(percentage > maxPercentage){
  136.         maxPercentage = percentage;
  137.         maxI = i;
  138.         maxJ = j;
  139.       }  // END OF IF-CASE FOR 'MAX' VALUES
  140.     }  // END OF IF-CASE THAT CHECKS THE DIFFERENT INSTANCES
  141.   }  // END OF SECOND FOR-LOOP
  142. }  // END OF FIRST FOR-LOOP
  143.  
  144. console.log();
  145. console.log();
  146. console.log("The 2 pAequors of the pAequorArray, that are the most related are those with ids: " + pAequorArray[maxI].specimenNum + " and " + pAequorArray[maxJ].specimenNum + ".");
  147. console.log("Max percentage = " + (100 * maxPercentage) + "%.");
  148. console.log("DNA Chain of pAequor with id = " + pAequorArray[maxI].specimenNum);
  149. console.log(pAequorArray[maxI].dna);
  150. console.log("DNA Chain of pAequor with id = " + pAequorArray[maxJ].specimenNum);
  151. console.log(pAequorArray[maxJ].dna);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement