Function Within Object Not Returning Anything

Hi, I’m currently making my way through the Mysterious Organisms project, and I’m stuck on the mutate method.

const pAequorFactory = (specimenNum, dna) => {
  return {
    specimenNum: specimenNum,
    dna: dna,
    mutate() {
       const mutator = {
        randomBase: returnRandBase(),
        mutation: pAequorObject.dna[Math.floor(Math.random() * 15)]
        }
        while (randomBase === mutation) {
          this.randomBase = returnRandBase();
        }
        return this.dna[mutation] = randomBase;
    }
  }
};

Mutate doesn’t return anything when the factory object is called, and doing Console.log inside of mutate returns undefined. What have I done wrong?

console.log returns undefined, but you wouldn’t use its return value that’s not what’s useful about it. Its usefulness is in printing values

If you are printing a value that you expect not be undefined, and it is undefined, then you’ve found the problem, haven’t you.

The thing is, I’m not sure what exactly is causing it to return undefined. All it returns is mutate: [Function: mutate].

When you say return do you mean print to screen? In the context of functions, return has special meaning and that meaning is unrelated to making things show up on screen.

If you printed something and the printed text was
[Function: mutate]
Then what you were printing was probably the function itself? That’s of course something you’d already know because you wrote code to print it. But is printing a function a meaningful thing to do? What would one usually do with a function? Call it, right? That’s pretty much the only operation you would do with a function.

> f = () => 5
> console.log(f)  // printing f: kind of pointless
[Function: f]
> console.log(f()) // printing the result of calling f, maybe f returns something interesting that can be printed
5

You’re right, I wasn’t calling my function correctly. I feel a bit silly now, but thank you so much for your patience and help.

Ok, so I called the function correctly, and it still returns [Function: mutate]. But the return statement I added also returns the DNA base that was randomly generated without changing and returning the rest of the DNA strand:

return pAequorObject.dna[this.mutation] = this.randomBase;

I apologize for asking for so much help, but I’m really struggling with this project and I have no idea how to figure it out on my own.

Are you printing or returning?

If you call a function then it’ll return a result. If you choose to, you can print that.

Similarly here, which action does this perform?

Are you trying to exit a function with a result value, or are you trying to assign a variable? Make up your mind.

I tried return pAequorObject.dna earlier, but it returned the DNA strand line by line instead of on one line and undefined: random DNA base.

Don’t.

Figure out what it’s supposed to do, and then use that.
You need to be a whole lot more deliberate about what you write.

You should probably start with a minimal piece of code where you know what everything does, then consider what you need to be adding on to that to get closer to your goal. Make a small change, test whether the change had the desired effect. You now have a new situation, what is the next change to make? Again, test it.

If you need some kind of action that you don’t know how to use, then you’ll have to stop and go learn it separately. Maybe there’s something you need to be reading or maybe you just need a separate file where you test it out with nothing else in it.

If you write a whole lot at once without testing anything inbetween it’s because you already have good control of it all and anything that could possibly go wrong is something you’re willing to deal with.

Dealing with multiple things that you don’t know what they do won’t work. How will you reason about one thing when you don’t know how the things around it work either?

You might for example want to start with an object that has an integer, and a method that increases the integer.

Separately from that you might want to figure out the more complicated operations in as much isolation from anything else as possible.

Are you able to generate random numbers? Are you able to select random elements? Can you successfully test whether two things are equal?

When you have some control over the smaller pieces you can start adding one of them into the other.

You’re still using the word return for printing.

Looking at the things in your code, there’s nothing in it suggesting that your code should be aware of “lines” or text, or screens.
So if the format of your output isn’t what you expected, that’s not a problem with the method because that’s not something your method is responsible for, is it?
So what is it responsible for? For testing you should probably be printing out the object, then call your method, and then print out the object again. Did it change in the way that it should? If the method is supposed to have a return value (not printing) then you would want to look at that as well. Quite often though, you would either create a new value and return it, or modify it and have no return value. Both changing something and returning something is a little bit weird, as if it can’t quite figure out what its purpose is, it should do one thing.

myObj = new Something()
print initial state: myObj
result = myObj.dothething()
print return value: result
print new state: myObj