Mystery organism - member function doesn't return mutated array

Hi there! I have been struggling to implement member function of pAequorFactory which is “mutate()”, when I run my code, It seems like mutate() isn’t invoked and I don’t get return array from the function. How can I fix this code?
Thank you in advance for your help!

// 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 = (number, array) => {
  return pAequor = {
    specimenNum: number,
    dna: array,
    mutate() {
      let randomIndex = Math.floor(Math.random() * 15);
      const newRandBase = returnRandBase();
      while (array[randomIndex] === newRandBase) {
        newRandBase = returnRandBase();
      }
      return this.dna.splice(randomIndex, 1, newRandBase);
    },
  };
};

const specimen1 = pAequorFactory(1, mockUpStrand());
console.log(specimen1);
const specimen2 = pAequorFactory(2, mockUpStrand());
console.log(specimen2);

I just ran your code and it returns mutated specimens. As far as I can tell it looks like its working. :grin:

❯ node cloud1561775757.pAequorFactory.js
{ specimenNum: 1,
  dna:
   [ 'T', 'T', 'G', 'C', 'A', 'T', 'A', 'A', 'G', 'A', 'A', 'T', 'G', 'C', 'C' ],
  mutate: [Function: mutate] }
{ specimenNum: 2,
  dna:
   [ 'A', 'T', 'A', 'G', 'T', 'T', 'G', 'G', 'G', 'T', 'G', 'A', 'A', 'A', 'A' ],
  mutate: [Function: mutate] }
1 Like

Thank you for your reply! I appreciate it.
I got the same result. I thought it’s not working because of this line. Maybe my I am wrong? Is this line expected result? Or if I invoke this mutate() function then I can get result…

mutate: [Function: mutate]

Looking through my code I don’t see one place where we are mutating anything, despite the fact we have a method to do that. It’s been a while since doing this module so I’m going off the code I wrote. It might be the mutate method was tested when we wrote it. Doesn’t seem to be in use, now. What am I missing?

1 Like

First of all I’m glad mtf pointed out that mutate() was not actually showing results.
I checked again numerous times after this and saw it was true.
ran it numerous times changing different things came up with the code changes and checks below for const pAequorFactory().

  • the code changes below show the mutation for sure and it shows at least one letter difference from original dna. Not sure if my wordage is correct but looks like when you run it the normal way console.log is returning the function object and not the results of it.
  • But yeah the normal way does not show the mutations.
  • I guess I just never noticed it and assumed the dna shown was already mutated.
  • the mockUpStrand() being random is what makes it hard and confusing to check lol.
const pAequorFactory = (number, array) => {
    return pAequor = {
      specimenNum: number,
      dna: array,
      mutate() {
        console.log('----> I\'m in here!')
        let randomIndex = Math.floor(Math.random() * 15);
        let newRandBase = returnRandBase();
        while (this.dna[randomIndex] === newRandBase) { newRandBase = returnRandBase();} 
        this.dna.splice(randomIndex, 1, newRandBase);
        return ['mutation: ', this.dna]
      },
    };
  };

  specimen1 = pAequorFactory(1, mockUpStrand());
  console.log(specimen1)
  console.log(specimen1.mutate());  <---- produces or shows mutation
❯ node cloud1561775757.paequorFactory.js
{ specimenNum: 1,
  dna:
   [ 'T', 'T', 'A', 'T', 'T', 'G', 'G', 'G', 'T', 'T', 'A', 'T', 'T', 'G', 'A' ],    //<----NOTE: T, T, A, T, 
  mutate: [Function: mutate] }
----> I'm in here!
[ 'mutation: ',
  [ 'T', 'T', 'A', 'A', 'T', 'G', 'G', 'G', 'T', 'T', 'A', 'T', 'T', 'G', 'A' ] ]    //<----note T, T, A, A  <-- mutation achieved
1 Like

Found my code, with these lines commented out, so we did run the method at some point…

//console.log(pq1.dna);
//console.log(pq1.mutate())
//console.log(pq2.dna);
//console.log(pq2.mutate())

Given the software we’ve developed we can run iterative scenarios of mutate->will survive. This is a project we can keep coming back to.

1 Like

Thank you for your advice! I appreciate it.

1 Like

Thank you for fixing my code. I appreciate your help!
I could fix my code and could check only one element has been changed and could see mutated array.