Mysterious Organism Challenge Project (JavaScript)

A console will output undefined when a variable hasn’t been defined (correctly) or a function body is missing the return keyword.

An object’s method is invoked as object.methodname() e.g., newOrganism.compareDNA() whereby newOrganism is the object and compareDNA() is the method belonging to that object. If a method that belongs to an object is called without reference to an (instance of that) object e.g., compareDNA(), the console/terminal will output an error.

Hope this adds clarity for you @andrewruboyianes0499 and others man :slight_smile: .

Best wishes in your journeeez!

@toddgallegos

2 Likes

Here’s my completed code (including the extra complementaryStrand() method!

I tend to forget about built-in iterators (I only barely remembered map() was a thing), so if anyone has any suggestions for me on how to clean up what I have, it’s greatly appreciated!

My solution:

This is my solution with Extension :slight_smile:

Hi Everyone! This was a fun challenge. Didn’t do the second bonus… (Had to move on, or I won’t hit my deadline.) Here’s my solution: Codecademy export · GitHub

It’s an interesting approach to select another random index if the random base matches the existing base. That adds an additional operation 25% of the time.

Hello, this is what i came up with. It took me time and energy, more than the other challenges…

I’m a bit concerned with the wording of the problem. It seems to me that question 4 is asking to make a mutate object function that selects a random base from a given DNA strand, then convert that to any of the 3 other possible bases. Based on solutions I’ve seen, It seems that the idea of others is to basically not return anything different if the randomly selected base is the same as the randomly generated base. Am I wrong in thinking this doesn’t fully fulfill the requirements of the question? What if I need a newly generated strand every time, even when my randomly selected base is the same as the randomly generated bases I wish to replace it with? I generated the below code which I believe corrects for this but wish it were more streamlined, less declaration of array variables. Is there a better way to do this in other words?

const pAequorFactory = (number,array) =>{

return {

specimenNum: number,

dna: array,

mutate(){

  const woA = ['T','C','G'];

  const woT =['A','C','G'];

  const woC = ['A','T','G'];

  const woG = ['A','T','C'];

  let woI = Math.floor(Math.random() * 3);

  let i = Math.floor(Math.random() * this.dna.length);

  if(this.dna[i] === 'A'){

    this.dna[i] = woA[woI]

  } else if(this.dna[i] === 'T'){

    this.dna[i] = woT[woI]

  } else if(this.dna[i] === 'C'){

    this.dna[i] = woC[woI]

  } else{

    this.dna[i] = woG[woI]

  }

  return this.dna

}

};

};

Hello coders!

This is my code for this challenge:

completed all the challenges.

Hi all !

Here is my code.
I really have to try to use filter and reduce built-in functions a lot more :smiley:

https://github.com/nanashi95/MysteriousOrganism

I had a similarly inefficient looking solution to that .mutate method so that it wouldn’t have the possibility of getting the same .dna again.
It uses a counter variable.

    mutate: function() {
      let randIndex = Math.floor(Math.random() * 15);
      let current = this.dna[randIndex];
      let randChoice = Math.floor(Math.random() * 3);
      let replacement;
      let j = 0;
      for (var c of bases) {
        if (c == current) {
          continue;
        }
        else {
          if (j == randChoice) {
            replacement = c;
          }
          j++;
        }
      }
      this.dna.splice(randIndex, 1, replacement);
      return this.dna;
    },

Although I was never sure if .mutate() should change the .dna for the object or return a new object that has the mutated .dna (so I went with changing the existing object since that was easier).

My solution’s gist link: thepragdev-mysterious-organism

I made some tweaks to the given helpers functions as well as added my helpers.

So I have more or less completed the project, but I was wondering, for the .willLikelySurvive() method, I used the following for loop with a counter:

willLikelySurvive() {
      let counterCAndG = 0;
      for (let i = 0; i < this.dnaArr.length; i++) {
        if (this.dnaArr[i] === 'C' || this.dnaArr[i] === 'G') {
          counterCAndG++;
        }
      }
      percentageOfCAndGStrands = counterCAndG / (this.dnaArr.length / 100);
      return percentageOfCAndGStrands >= 60;
    }

Comparing this to the solution using the .filter() method:

willLikelySurvive() {
      const cOrG = this.dna.filter(el => el === "C" || el === "G");
      return cOrG.length / this.dna.length >= 0.6;
    }

I did some looking up and the differences in performance… and I got a little lost with it and wondering if anyone could clarify, the for loop seems to be better performance wise, but the .filter() method doesn’t stop if another operation is triggered, i.e the stack will be paused using a for loop but using .filter() the call will continue alongside another operation. Is this right or are there other reasons to use .filter() over a for loop?

Hi learners!

Since it is the first project I was able to do without checking the solution :smile:, here is my code :smile:

Mysterious-Organism-Challenge-Project–JavaScript Exercise

My concerns are detailed after and would enjoy feedback regardless of what decade you read this. I think the for loop inside for loop using the index is the most efficient. I used map as my outer for loop, I’m sure leaving wiggle room to simplify. I tried a recursive function too. That ended horribly overpriced. Moving on to the survivability in initialization. The do while loop seems to be the simplest method to initialize the 30 specimen array based on the project requirements. If I were coding this for live usage I would change the random function to a seeded random that returns more survivable base 15 strands when populating. Probably populate 60% C and G and then random fill the remaining with a weighted base index. Then randomize (shuffle) the array. This would be less cost than do while. I think the do while loop in this case causes a bottleneck in initialization of the specimen object array. Curious what other solutions were used to initializing all 60% ‘C’ and ‘G’ specimens.

Hi :slight_smile:
This is my solution!

Hey all, this was a great problem that showcases how little code you actually need when solving problems if you take the time to think through them properly and use the right tools.

Here is my solution!

Just finished this today! Was a challenge, but I feel good about my solution. Check my solution here