Mystery Organism Project

Working through the Mystery Organism project however it fails to run in the terminal. Fails to run as in it just gets hung up and I never receive a result. I have a feeling there is an issue between lines 41 - end of main.js but I never found anything.

Here is the 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
}

const pAequorFactory = (specimenNum, dna) => {
  return {
    specimenNum,
    dna,
    mutate() {
      const randomIndex = Math.floor(Math.random() * this.dna.length);
      let newBase = returnRandBase();
      while (this.dna[randomIndex] === newBase) {
        newBase = returnRandBase();
      }
      this.dna[randomIndex] = newBase;
      return this.dna;
    },
    compareDNA(orgOther) {
      const similar = this.dna.reduce((acc, cur, idx, src) => {
        if (src[idx] === orgOther.dna[idx]) {
          return acc + 1;
        } else {
          return acc;
        }
      }, 0);
      const sharedPercentOfDNA = (similar / this.dna.length) * 100;
      const percentWith2Decimal = sharedPercentOfDNA.toFixed(2);
      console.log(`Specimen ${this.specimenNum} and specimen ${orgOther.specimenNum} have ${percentWith2Decimal} DNA in common`);
    },
    willLikelySurvive() {
      const baseCG = this.dna.filter(element => element === "C" || element === "G");
      return baseCG.length / this.dna.legnth >= 0.6;
    },
  }
};

const specimensSurvived = [];
let specimenCounter = 1;

while (specimensSurvived.length < 30) {
  let newOrganism = pAequorFactory(specimenCounter, mockUpStrand());
  if (newOrganism.willLikelySurvive()) {
    specimensSurvived.push(newOrganism);
  }
  specimenCounter++;
}

console.log(specimensSurvived);

If you’ve got a bunch of things that you think should be happening and in total amount to some result and termination of your program … but that isn’t happening, then, maybe you’d want to start making observations about what is being done? console.log can tell you a lot.

For example if you have a loop, then you might want to know things like whether the loop at all ran, whether it finished, and if it did the first but not the second, then, does the thing that the stop condition is looking at change in the way that you expected? If it does change as expected and reaches what would terminate the loop, but the loop doesn’t terminate, then clearly the problem is the stop condition. Or if it doesn’t change, then the problem is whatever should have changed it, and then you’d start investigating that in a similar manner.

Hello everybody, I am working on the point 5 of this project and cannot get through it. When I am trying to check if my code works all the time I get error. I feel like I have already tried to change everything but nothing helps. I am not really sure how to give as a parameter a new object so maybe this is why it doesn’t work…

This is the last version of my code, could somebody tell me what am I doing wrong?

// 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 = (number, newStrand) => {
  return pAequor = {
    specimenNum: number,
    dna: newStrand,
    
    mutate() {
      let indexOfBaseToMutate = Math.floor(Math.random() * 15);
      const baseToMutate = this.dna[indexOfBaseToMutate];
      const mutatedBase = returnRandBase();
      if (mutatedBase !== baseToMutate) {
        this.dna.splice(indexOfBaseToMutate, 1, mutatedBase);
        return this.dna;
      } else {
        this.mutate();
      }
    },
    
    compareDNA(newpAequor) {
      let object1 = this.pAequor.dna;
      newpAequor = pAequorFactory();
      let object2 = newpAequor.dna; 
        
      let commonDNAPercent = 0;
      
      for(let l=0; l<15; l++) {
        for(let j=0; j<15; j++) {
          if (object1[l] === object2[j] && l===j) {
            return commonDNAPercent + 6.67;
          } else {
            return commonDNAPercent + 0;
          }          
        }
      };   
      console.log(`${this.pAequor.specimen} and ${newpAequor.specimen} have ${commonPercent} % DNA in common`);
    }   
    
  };
};

              

/*for test (it works):
console.log(pAequorFactory(1, mockUpStrand()));
pAequor.mutate();
console.log(pAequor.dna);*/

//for test (it doesnt work):
pAequorFactory(1, mockUpStrand());
pAequorFactory(2, mockUpStrand());

console.log(this.pAequor.compareDNA());

start by replacing this with a description of how it is currently behaving accompanied by how it should be behaving instead

those two descriptions also probably say a lot about what part of the code is responsible, you can now start making observations about what actions were carried out to reach the bad result while keeping in mind which actions you meant should happen

presumably that suggests you tried to do something impossible, what and where, that’s something that can be followed to the cause of the problem

blah.js:66
console.log(this.pAequor.compareDNA());
                         ^

TypeError: Cannot read property 'compareDNA' of undefined

That says you are trying to use something that doesn’t exist, it says which thing doesn’t exist, and it says where you’re trying to use it. Should it exist? If so, then the problem is with the code that should have defined it. Or if not, then don’t use it. Maybe you meant to use something else.