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.

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

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

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!

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.