Mysterious organism https://www.codecademy.com/practice/projects/mysterious-organism

I have a problem when i test mutate function it always prints out Undefined. I know there is a problem in this function but I am not sure where. This is the 4th step.

pAequorFactory= (number, array)=>{

const result = {

specimenNum: number++,

dna:array

};

creatingDna =()=> {

const getingDna= newStrand;

dna.push(this.getingDna);

}

return result;

mutate =()=>{

for(j=0; j< 15; j++){

if(this.dna[j]= ‘A’){

const returnNew = () => {

const dnaBase = [‘T’, ‘C’, ‘G’];

dnaBase = dnaBase[Math.floor(Math.random() * 3)];

this.dna[j].splice(this.dna[j], 1, this.dnaBase);

return this.dna;

};

};

};

};

};

It’s because you’ve got unreachable code:

I’ve formatted it here, for future reference if you press the ‘preformatted text’ icon then paste your code it will be easier to read:

pAequorFactory = (number, array) => {

  const result = {
    specimenNum: number++,
    dna: array
  };

  creatingDna = () => {
    const getingDna = newStrand;
    dna.push(this.getingDna);
  }

  return result;

  mutate = () => {
    for (j = 0; j < 15; j++) {
      if (this.dna[j] = ‘A’) {
        const returnNew = () => {
          const dnaBase = [‘T’, ‘C’, ‘G’];
          dnaBase = dnaBase[Math.floor(Math.random() * 3)];
          this.dna[j].splice(this.dna[j], 1, this.dnaBase);
          return this.dna;
        };
      };
    };
  };
};

Your mutate function can’t be reached because of the return result, either move the return to right before the last closing brace or remove the result declaration and just do return there .i.e.

pAequorFactory= (number, array)=>{
   return {
       specimenNum: number++,
       dna:array,
       [...]
   }
};

Your methods should also be part of the object creation. You’re closing it too early. The creatingDna and mutate methods are in the factory but should be part of the object.

1 Like

It still doesn’t work. I did put everything into one object and change return result to:

pAequorFactory= (number, array)=>{
   return {
       specimenNum: number++,
       dna:array,
       [...]
   }
};

But still mutate function prints undefined. Plus now as a result computer prints:

{ specimenNum: 1,
  dna: [ 'C', 'A', 'T', 'T', 'A', 'C', 'A', 'C', 'T', 'C', 'G', 'G', 'G', 'G', 'C' ],
  creatingDna: [Function: creatingDna] }
undefined

This is how the code looks like:

const creation ={

pAequorFactory (number, array){
    return {
  specimenNum: number++,
  dna:array,
  creatingDna () {
const getingDna= newStrand;
dna.push(this.getingDna);
  }
    };
  
mutate=()=>{
  const mutatedDna = this.dna.slice();
for(let j = 0; j < 15; j++){
if(this.mutatedDna[j]= 'A'){
  let returnNew = () => {
  const dnaBase = ['T', 'C', 'G'];
  this.mutatedDna[j].splce(this.dna[j], 1, dnaBase[Math.floor(Math.random() * 3)]);
return this.mutatedDna;
};
};
};
};
}
};

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

I’d say maybe revisit the lesson on object factories and for loops and make sure you’re happy with those and how to add methods to an object

For example, to define a new factory object it’s the same way as an arrow function

const pAequorFactory = (number, array) => {
  return {
    specimenNum: number++,
    dna: array,
    [...]
  }
};

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