TypeError

Hello all! Here is a link to the project Mysterious Organisms Link.

The step I’m on is:

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 getting the following error:
TypeError: pAequorFactory.mutate is not a function

Any help would be very much appreciated!

// 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 = (specimenNum, dna) => {
    return {
      specimenNum : specimenNum,
      dna : dna,
      mutate() {
        // generates a random DNA base
        var mutatedDnaBases = ['A', 'T', 'C', 'G']
        return mutatedDnaBases[Math.floor(Math.random() * 4)]
        //Compares the randomly selected base to the 1st base
        if (mutatedDnaBases === returnRandBase) {
          //runs the generation of a random DNA base again
          mutatedDnaBases[Math.floor(Math.random() * 4)]
        } else {
          //if the mutated base is different from the return base it logs it to the console
          console.log(mutatedDnaBases)
        } 
      },
    } 
}
console.log(pAequorFactory.mutate())












Hi Vicky (we’re having a lot of contact on here!),
Unhelpful comment: it indeed isn’t a function.

More helpful comment: The factory function you’ve made makes objects that have that function.

The key to the problem is actually stated in the task:

For example, you could put const newDNA = pAequorFactory(1, ['T', 'C', 'G', 'A', 'T', 'C', 'G']); where you’ve tried to call the function and it would create a new object from the factory function - then, that object would have the mutate() function.

const newDNA = pAequorFactory(1, ['T', 'C', 'G', 'A', 'T', 'C', 'G']);
console.log(newDNA.dna);
//logs ["T", "C", "G", "A", "T", "C", "G"] to the console 
newDNA.mutate();

Hope that helps!

Hi @jonrosk089! Thank you so much for your reply! So grateful for all your help! Haha we are having a lot of contact! I’ve been doing JavaScript for about three weeks and pretty new to coding! Hence all the questions!

So, can I keep some of my code? Because the instruction was to log only one letter, the base letter? My code seems to do this.

Also, I see you’ve put returned object in bold. Indeed, I missed that part. Have I not placed the function in the correct place?

If you can’t tell - I’m still confused!!!

Hey Vicky,
Don’t worry, I’ve been doing it for about 3 months and at some point this stuff just clicks. Wait until you get to requests, that’s where my brain is being a bit tested…!

You can keep all of your code. I’ll try to explain a bit better.

Your factory function is good as it is - the point of it is that it builds objects for you without you having to start from nothing each time. So, you plug in the number and the DNA and it creates an object with those key: value pairs, and the object also has the mutate function. The factory function is simply there to make objects; otherwise, you’re always interacting directly with the objects you’ve used it to produce. Is that clearer?

Re the last point, that isn’t exactly what the task is asking for - nothing has to be logged to the console. It’s just giving you an example of the functionality of mutate - one letter is changed at random, which could be the first letter.

1 Like

Hi @jonrosk089, thanks so much again for your reply.

Oh dear! The hardest part is yet to come!

Am I not placing my method mutate() in the correct place? Is that why it’s not working? I’ve tried to close the previous section off with curly braces and then put a curly brace after the mutate() but that brings back a syntax error.

Yes, you’re very right! So the DNA contains 15 bases, or 15 letters, and the mutation changes one of those letters? Is that right?

Also @jonrosk089 , what does the ‘returned object of the factory function’ mean? Thank you so much, again.

Hi Vicky,
Leave your code as it was when you first posted the problem and create a new object like this:

const newThing = pAequorFactory(1, ['T', 'C', 'G', 'A', 'T', 'C', 'G']);

Then you can call newThing.mutate() to use your function.

1 Like

Hi @jonrosk089, I will do that. Bit I’m not sure why… why are there more than 4 letters in that array?

Re the returned object - it is the object that the factory function created for you. Think about it like a robot that makes folders - you use the robot to make the folders, but then you access the folders directly, you don’t ask the robot about them because they’ve left the robot’s sphere of influence.

1 Like

Thank you! I see… so it’s within the curly braces of the object?

Yeah, as you had it originally. Basically it’s within the returned object, or to put it another way, it’s like something with functionality - like a calculator - that’s included in the folder the robot made.

1 Like

@jonrosk089. I’m getting it! Thank you! But how about this above? Last question!! I promise!!

No worries - I was just giving a simple version that you can use to check that it’s working by copying and pasting. If it looks like it’s ok, delete my line and carry on with the task!

1 Like

@jonrosk089 Thank you soooo much! Forever grateful!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.