Mysterious Organism - Factory Function Method

Before I start, major thanks to any help with these questions!
I have basically two questions about how to return objects from a factory function, and how to create methods in them. When I console.log my values, it says “Specimen One” and gives [an array of nucleotides], which is what I want it to log, but they are written in the console as {Specimen One", [an array of nucleotides]}. Should it be showing the curly brackets?
Next question. How do you write a function in a factory function? In this example, would it be:

Mutate: Mutate() { }

or just

Mutate()

or something completely different?

I’ve tried writing the function a few different ways, but it logs mutate: [function mutate] every time I call the object (without calling the function on it), and when I call the function on the object, it just console.logs Function: Mutate.

Here is a link to the exercise:

(https://www.codecademy.com/journeys/back-end-engineer/paths/becj-22-software-engineering-foundations/tracks/becj-22-javascript-syntax-part-ii/modules/wdcp-22-mysterious-organism-c8603589-5297-450f-9af0-366ee62f82d6/projects/mysterious-organism)

And here is my code for the exercise:
**Also this is a syntax question I have to work on the actual function I wrote. I just want to make sure I am syntaxing everything correctly before I keep working on the actual function.

// 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, dna) => {
return {
specimenNum: num,
dna: mockUpStrand(),
mutate() {
const dnaBases = [‘A’, ‘T’, ‘C’, ‘G’]
for(let i = 0; i < dnaBases.length; i++) {
if(i in dnaBases[Math.floor(Math.random() * 4)] != mockUpStrand[-1]) { mockUpStrand.pop();
mockUpStrand.push(i);
return mockUpStrand;
}
}
},
}
}

const specimenOne = pAequorFactory(1, mockUpStrand());
console.log(specimenOne);
//Second console log was to make sure that it returns the same set of nucleotides when I run the code again.
console.log(specimenOne);
console.log(specimenOne.mutate);

There is your, [function mutate]. Your method is returning a function object.

Thank you for the response. My question was how to syntax the function in the factory function. Would I write:
specimenNum: num
dna: mockUpStrand
Mutate: Mutate()

or
specimenNum: num
dna: mockUpStrand
Mutate()

basically how would I syntax the function?
Thanks again for your help!

Consider what the mutate method is meant to return: A mutated strand? That would mean being the same ‘shape’ as the dna property of the object.

const pAequorFactory = (specimenNum, dna) => {
  return {
    specimenNum,    //  destructured assignment
    dna,
    mutate() {
        // mutation code
        return this.dna
    }
  }
}

Note: Using destructured assignment in this setting is worthwhile syntactic sugar. Whatever values we pass into the function will be given to properties of the same name as the parameter. Pretty cool, eh?

{
  specimenNum: 1,
  dna: [ 'G', 'C', 'A', 'C', 'G', 'T', 'C', 'C', 'A', 'A', 'C', 'C', 'T', 'A', 'A' ],
  mutate () {

  }
}

Thank you that is helpful.

1 Like