Mysterious Organism Challenge Project Help with Objects & Methods

I’m having some trouble with Step 4 of the code challenge. I need to make a method within an object that randomizes the DNA base of each element in the DNA array, and replace each instance of the current DNA base with the randomly generated DNA base. The base MUST be replaced with a different DNA base. My problem is that my code debugger says that my mutate method within the pAequorFactory object is an unexpected identifier. That usually means that there is some syntax error, but I can’t find my mistake. Can anyone help? Code is below:


// 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;
};

// Factory object below should create a specimin with a random DNA sequence,
// and if you invoke the mutate() method, it should replace every element in the dna array with a new base.
// If the base is the same as the base it is replacing, the method should try a different DNA base to replace it with.
const pAequorFactory = (specimenNum, dna) => {
  var obj = {
    specimenNum: specimenNum,
    dna: dna,
    const mutate() {    //I get a Syntax Error here saying that mutate is an Unexpected identifier. Does anyone know why?
      let newDNABase = returnRandBase();
      let dnaBases = ['A', 'T', 'C', 'G'];
      for (let i = 0 ; i < dna.length ; i++) {
        do {
          newDNABase = returnRandBase();
        }
        while (this.dna[i] === newDNABase);

        this.dna[i] = newDNABase;
      }

      return this.dna;
    }
  };
return obj;
};

console.log(pAequorFactory(1, mockUpStrand()));
pAequorFactory.mutate();
console.log(pAequorFactory.dna);

Error reads:

const mutate() {
      ^^^^^^

SyntaxError: Unexpected identifier

Thanks in advance!

David

You cannot define a method of an object with the const keyword. Just write:

var obj = {
    specimenNum: specimenNum,
    dna: dna,
    mutate() { 
      //...
    }
}

Then you need to create an instance of the object with the factory function. Once that’s done you can apply its method:

const pAequorInstance = pAequorFactory(1, mockUpStrand());
pAequorInstance.mutate();
console.log(pAequorInstance.dna);
1 Like

Your solution worked perfectly. Thanks so much!

1 Like