Mysterious Organism Project - stuck on #4

I’ve been away from JavaScript for several days, and it’s like the whole language disappeared from my brain. When I came back to practice, the Mysterious Organism project came up on my dashboard. Link: https://www.codecademy.com/practice/projects/mysterious-organism

I got stuck creating the mutate() method, #4, the instructions state:

Your team wants you to simulate P. aequor ‘s high rate of mutation (change in its DNA).

To simulate a mutation, in pAequorFactory() ‘s returned object, add the method .mutate() .

.mutate() is responsible for randomly selecting a base in the object’s dna property and changing the current base to a different base. Then .mutate() will return the object’s dna .

For example, if the randomly selected base is the 1st base and it is 'A' , the base must be changed to 'T' , 'C' , or 'G' . But it cannot be 'A' again.

I’m really not sure where to go, or if I’m even on the right track. I could easily look up the code, but I don’t want to do that for a multitude of reasons.

Could someone please take a look at the code I’ve written for mutate so far and share some feedback, ideas about what I need to make this work?

// 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 = (num,dnaBasesArr) => {
  return {
    specimenNum: num,
    dna: dnaBasesArr,
    mutate { // randomly select base in dna then change current base to a different base then return object's dna
      let randomBase = Math.floor(Math.random() * this.dna.length); // randomly select base in dna
      let changedBase = returnRandBase(); 
        // this is where I'm not sure what code to put in...seems like and if/else statement would be a logical choice, but what am I putting into the if() and else statements???
      return this.dna; // not sure if this return declaration is correct, but that's what the instructions say pretty much verbatim
    }
};



There are a couple ways that you can do this really. I like your way of using the previously created returnRandBase(), if you want to go like that, then think of how you would go about dealing with the 2 scenarios; If the returned base is the same as the current one, and if the returned base is different.

The way I did it was slightly different, and it involved creating multiple if statements using base arrays of length 3 instead of length 4. I’ve tried to be pretty vague so you can try working it out yourself, but I think you’re on the right track. Think on which way you’d prefer to do, and then hopefully my tips can help, if not I can be a bit more specific.

1 Like

Hm okay, that helps a little @adamgaffney137183916, thanks. Now my code looks like this.

...
     let changedBase = returnRandBase(); 
      if (randomBase === changedBase) {
        //what do I want to happen? should I just console.log changedBase? why?
      } else {
        //do this...do what?
      }
      return this.dna;
...

Not really sure what I should put into the if/else statements. The instructions don’t lend themselves to a clear direction, at least to me. Help?

Based on the instructions, we know about the mutation that

Therefore if the returned base is equal to the current base, we would want to “roll again” as it were, until we get a base that is not equal to the random base. So to continue with this method, I would recommend using some sort of loop, that checks:

  1. if changedBase === this.dna[randomBase]
  2. if yes, loop and check with a new changedBase
  3. if not equal, then replace this.dna[randomBase] with the new base

Important to remember that in your current code, randomBase is just a random number, you want to use that to then get that element from the dna strand, hence `this.dna[randomBase].

1 Like

Your breakdown is definitely helping me understand.

Question: why do you recommend using this.dna[randomBase] as opposed to just randomBase? randomBase is a variable, and this notation seems to indicate to me a reference to an array (right?)

1 Like

Using randomBase as a variable is fine, however going by your definition below

randomBase is an integer value, between 0 and the length of this.dna.length, in this case 15. So randomBase on this definition is just an integer. You want to use this integer to get a random letter from your dna strand to compare with changedBase. So you can either slightly change randomBase to be a reference randomBase = this.dna[Math.floor(Math.random() * this.dna.length)], or you need to use the integer randomBase as a reference later on, hence the this.dna[randomBase]. Does that make sense?

Perfect sense. Thank you.

And thanks for demonstrating another way to express this variable. After looking at what you wrote: randomBase = this.dna[Math.floor(Math.random() * this.dna.length)], I think I prefer this. It seems like a more complete way to write it, plus using it later in the code block seems clearer to me.

So let’s say I used the reference instead of the integer. How would that affect the rest of the code? I presume I could then just declare randomBase as opposed to this.dna[randomBase], but does it also change the return call (or anything else)?

No problem at all! You are correct in saying you would just use randomBase as opposed to this, since randomBase should not be one of the bases in your current dna array. Nothing else should need to change honestly, I think it’s just that! Let me know if that gets it working.

1 Like

I’m still struggling with the loop and code that goes inside it, but I’ll get there. Thanks again for your time and input.