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:

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++) {
  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 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.

i also struggle bit on that one - usualy i want to avoid to write code the way i once learned (like couple of loops, nested if/ else and even more loops to compare again etc) , because those array methods are there for a reason.
i knew it could be done in just some few lines:
i wouldnt guarantee it is 100% perfect or even correct, but i know this is at least a bit more appropriate to a daily output of a JS developer:
im open for any ideas, thank you :slight_smile:

onst pAequorFactory = (number, arr) => {
  return {
    specimenNum: number,
    dna: arr,
    mutate () {
        this.dna = => {
          let newBase = '';
          do {
            newBase = returnRandBase();
          } while(base === newBase) 
            return newBase;

From the directions:

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

Using map causes .method() to mutate every base, but according to the instructions it should only mutate one randomly selected at a time. On the other hand, if you want to completely change the entire DNA strand, that’ll do it.

oh, thank you! ofcourse you’re right!
■■■■ …
2 things are important:
1: read and listen carefully to instructions
and 2: read and listen carefully to instructions!

then it could just something classic be like :

    mutate () {
        /*this.dna = => {
          let newBase = '';
          do {
            newBase = returnRandBase();
          } while(base === newBase) 
            return newBase;
        let randomBase = Math.floor(Math.random() * this.dna.length);
        let originBase = this.dna[randomBase];
        do {
          this.dna[randomBase] = returnRandBase();
        } while(originBase === this.dna[randomBase]);



1 Like

You’ve got it. That one works perfectly. However, I’d switch the do… while loop to a regular while loop, since your current code will always execute .returnRandBase() on the randomBase twice, even if the generated base was different from the original base the first time.