[expression] is not a function

Hi all,

I am doing the Mysterious Organism project and I have a problem with this code


// 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
}
//Array to save object and check the same object is not created twice
const createdSpecimen = [];

const pAequorFactory = (num, DNA) => {
  if (createdSpecimen.includes(num) === true) {
    return 'Object already exist';
  } else {
     createdSpecimen.push(num);
     return {
      specimenNum : num,
      DNA : DNA,
      get gDNA () { return this.DNA },
      mutate () {
      let baseToChange = Math.round(Math.random() * DNA.length);
      let oldBase = this.DNA[baseToChange];
      while ( oldBase === this.DNA[baseToChange]) {
        oldBase = returnRandBase();
      }
      this.DNA[baseToChange] = oldBase;
     },
     willLikelySurvive() {
       let amountRightBase = 0;
       for (z=0; z < 16; z++) {
         if (this.DNA[z] === 'C' || this.DNA[z] === 'G' ) {
           amountRightBase++;
         }
       }
       return amountRightBase >= 9 ? true : false;
     }
   }
  }
}

const createAequor = quantity => {
  const arraySurvivors = [];
  while (arraySurvivors.length < quantity) {
    let numero = arraySurvivors.length + 1;
    let tempObject = pAequorFactory(numero, mockUpStrand());
    let prova = tempObject.willLikelySurvive();
    if (prova === true) {
    arraySurvivors.push(tempObject);
    } 
  }
  return arraySurvivors;
};

console.log(createAequor(30));

I get the error : TypeError: tempObject.willLikelySurvive is not a function at createAequor

I feel like there is something wrong with the if statement: when I comment it off, other than not saving the right objects in the array, the functon works. After hours I could not work out what I have done wrong. Thanks for any help!

We’ll need to see the complete body of code to track for errors.

Hi, I have added all the code… Thanks for checking it :slight_smile:

1 Like

Part of finding a problem is stripping away things are not needed, at least for the time. We could start with the above check. More still, consider, should a factory function have side effects? The above does, which it shouldn’t. Go back to the drawing board and find a way to remove any side effects.

Thanks for finding the problem Roy :slight_smile:

I’ll try to solve the issue and keep in mind your advise for all the future issues I’ll ecounter!

1 Like

Let us know how you make out with this. We’re still here if help is needed.


Not sure if this helps, my returned object has four methods,

  • mutate
  • compareDNA
  • willLikelySurvive
  • complementStrand