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:
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);