Mysterious Organism Challenge Project (JavaScript)

Hello everyone. This is my project.

Hello,

Finished :sweat_smile:

Github repo

Excited to share my latest project - the Paequor DNA Simulation Project! :dna::sparkles: This has been a journey into the fascinating world of genetic simulation, where I’ve explored the fundamentals of JavaScript to model the behavior of fictional Paequor organisms and their unique DNA strands.

This project allowed me to dive deep into coding concepts such as loops, conditionals, object-oriented programming, and much more. I’ve implemented features for creating pAequor instances with unique identifiers, mutating their DNA, and comparing the genetic material between instances to showcase their similarities and differences.

Whether you’re a coding enthusiast, interested in genetic simulations, or just curious about my coding journey, I invite you to check out my repository on GitHub:

Feedback, suggestions, and collaborations are always welcome! Let’s connect and explore the endless possibilities of coding and simulation together.

1 Like

https://www.codecademy.com/workspaces/65cbe60f6065876fd2cd016b

Here is my code, done with a little help from Copilot

1 Like

Hey … was checking your code out of curiosity and is it possible that there’s a mistake line 68 " if (this.dna[i] !== otherPaquor.dna[i])" ? I think it should be === and not !==. Also line 73, I wouldn’t use Math.floor because if it’s 68,9%, it would write 68%. In the exercise, they say we could use the .toFixed() method to make it mathematically correct. :slight_smile:

Wish there was a video on the ‘Get unstuck’ section. There are always helpful.

Oh thanks, I missed this. The code was working so I guess I didn’t check
Here is the update. Codecademy export · GitHub

1 Like

HI, this is my solution to Mysterious Organism;

Its not perfect but it is a learning curve to see how others have approached the problem, and hopefully my approach can help others as well.

comments welcome

Jim.My Mysterious organism Solution - on github

Hi fam, just wanna share my answer here. If you have a chance to look, please me know your thought. Happy coding!

1 Like

Here is my mystery organism file, happy to hear any comments.

My code

This was a tough one!

Hi everyone! Can anyone please help me understand why compare DNA alaways returns a 100 whenever I pass the return value of the .mutated() method to the .compareDNA() method.

// Factory function
const pAequorFactory = (num, arr) => {
  return {
    specimenNum : num,
    dna : arr,
    mutate () {
      // Creates a random index
      const randBaseIndex = Math.floor(Math.random()*arr.length);
      let mutatedBase = '';
      if (mutatedBase !== returnRandBase()){
        mutatedBase = returnRandBase()
      }
      this.dna[randBaseIndex] = mutatedBase;
      return this.dna;
    },
    compareDNA (pAequor) {
      let similarity = 0;
      pAequor.forEach((base, i) => {
        if (base === this.dna[i]) similarity++;
      });
      return (similarity/this.dna.length)*100;
    },
    willLikelySurvive () {
      const survivalChance = this.dna.filter(base => {
        if (base == 'C' || base == 'G') return base
      });
      const survivalRate = (survivalChance.length/this.dna.length)*100;
      if (survivalRate >= 60) return true;
      return false;
    }
  }
}

When I assign the values to variables and log it, it clearly shows a difference but when logging. While it works as required, I’m just really curious about this.
Also how do I go about enabling chaining methods i.e. sample.mutate().willLikelySurvive() or sample.mutate().mutate()

Are you trying to use .mutate() to change the .dna of the existing object, or to create a new array?

The way you have it in the code above is that .mutate() changes the .dna of the object,
and the method returns the array stored as .dna
so comparing what is returned from .mutate() with the .dna would be comparing an array to itself.

Also, you call returnRandomBase twice but it could be returning a different string each time;
it might make sense to only call it once, but store what it returns as a variable.

If you intend to create a new array where only one of the values is different, you could use the
.with method for JavaScript arrays

In order to have chaining for a method, you’d have to return the object in its method
by doing
return this;

1 Like

Thanks for reviewing my code. I have modified the method as follows.

    mutate () {
      // Creates a random index
      const randBaseIndex = Math.floor(Math.random()*this.dna.length);
      // Declares value of selected index based on randBaseIndex
      const selectedIndex = this.dna[randBaseIndex];
      // Initializes temp base replacement
      let mutatedBase = '';
      do { // Generates random base until it doesn't match original
        mutatedBase = returnRandBase();
      } while (mutatedBase == selectedIndex);

      // Directly mutates dna array at index 'randBaseIndex'
      this.dna[randBaseIndex] = mutatedBase
      return this.dna;
    },

As the requirement for this part of the project is to return .dna I chose to manipulate the original property rather than just creating a new array. I understand now that doing this, I will not be able to chain the method as I liked. I also created a version where I can do method chaining but that was a little out of scope of the project requirement.
Thanks.

Hi, here’s my code. I made some print statements at the end that use all of our methods.

// Returns a random DNA base const returnRandBase = () => { const dnaBases = ['A', 'T', 'C', 'G'] return dnaBases[Math.floor(Math.random() * 4)] } // Returns a random single strand of DNA containing 15 bases const mockUpStrand = () => { const newStrand = [] for (let i = 0; i < 15; i++) { newStrand.push(returnRandBase()) } return newStrand } // main function pAequorFactory(num, arr) { return { specimenNum: num, dna: arr, mutate() { console.log(`Current strand: ${this.dna}`) const randIdx = Math.floor(Math.random() * 15); let randBase = returnRandBase(); while (randBase === this.dna[randIdx]) { randBase = returnRandBase(); }; this.dna[randIdx] = randBase; console.log(`Mutated base ${randBase} located at index ${randIdx}`); return this.dna; }, compareDNA(pAequor) { const otherDNA = pAequor.dna; let count = 0; for (i = 0; i < 15; i++) { if (this.dna[i] === otherDNA[i]) { count++; }; }; // console.log(this.dna.join('')); // console.log(pAequor.dna.join('')); // console.log(`${count} bases in common out of 15`); console.log(`Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${(count/15)*100}% DNA in common.`); }, willLikelySurvive() { let count = 0; this.dna.forEach(base => { if (['C','G'].indexOf(base) !== -1) { // console.log(base) count++; }; }); // console.log(count/15); if ((count/15)*100 >= 60) { return true; } else { return false; }; } }; }; // specimen1 = pAequorFactory(1, mockUpStrand()); // specimen2 = pAequorFactory(2, mockUpStrand()); // TESTS // console.log(specimen1.mutate()); // specimen1.compareDNA(specimen2); // console.log(specimen1.willLikelySurvive()); //make array of 30 specimens let specimens = [] for (i = 1; i <= 30; i++) { specimens.push(pAequorFactory(i, mockUpStrand())); }; // print specimens and method results let count = 1 specimens.forEach(specimen => { let nextSpecimen; if (count < specimens.length) { nextSpecimen = specimens[count]; } else { nextSpecimen = specimens[0]; }; console.log(`Specimen ${specimen.specimenNum} DNA: ${specimen.dna.join('')}`); console.log(`Likely to survive: `, specimen.willLikelySurvive()); specimen.compareDNA(nextSpecimen); console.log(specimen.mutate()); console.log(); count++; })

Hi @iharland12 it looks that you forgot that the array of 30 specimens should have at least 60% chance to survive.
Also I thinks that the part of the if for willLikelySurvive is wrong because you are searching for an array [‘C’,‘G’] in a letter, because base is a letter in each iteration of the forEach

Hi guys, this will be my solution for the Mysterious Organism Challenge Project https://github.com/aloayzab88/codecademy-exercises/blob/main/js/arrays-loops/mysteriousOrganism.js
I added a count for how many pAequories didn’t make it :frowning: just for fun.
Also added the 9.1 extra, I don’t know how to even begin with 9.2

1 Like

Hello peers, this is my solution for this challenge:

Mysterious Organism Code GitHub

I am all ears for suggestions, errors that I didn’t consider and bugs. Rip apart my code and tell me if anything is wrong. @aloayzab88 I know that if there’s someone that will find anything is you, so rip apart my code! People like you push us to improve :otter:

1 Like

Hi @ricard0g, good job, glad to be able to help fellow devs.
Your code looks good, but remement the instructions said to store in an array 30 pAequors that will survive, not just 30 specimens.
Also there is room for some improvement dealing with returning true or false. For example in the willLikelySurvive method there is no need to return true o false explicitly because the expression will evaluate to one of those values ​​on its own. Same for the compareDNA method and the while expression, you could just compare one dna length as any pAequors dna will have always 15 bases.
Btw very clever way to count the C y G bases here.

let cAndGsArr = this.dna.filter(base => {
    return base === 'C' || base === 'G'
});