Mysterious Organism project keeps throwing an undefined

I made it to Step 5 in the Mysterious Organism project and finally got the comparison to run the right percentages, but now it’s throwing in an undefined at the end and I can’t figure out why.

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

function pAequorFactory(num, array) {
  return {
    specimenNum: num,
    dna: array,
    mutate() {
      let i = Math.floor(Math.random() * this.dna.length);
      let original = this.dna[i];
      let bases = ['A', 'T', 'C', 'G'];
      bases.splice(bases.indexOf(original), 1);
      let mutator = bases[Math.floor(Math.random() * 3)];
      return this.dna.splice(i, 1, mutator);
    },
    //Compare DNA strands.  Problem area
    compareDNA(pAequor) {
      let sameBases = 0;
      let totalBases = this.dna.length + pAequor.dna.length;
      console.log(this.dna);
      console.log(pAequor.dna);
      for (let i = this.dna.length - 1; i > 0; i--) {
        if (this.dna[i] === pAequor.dna[i]) {
          sameBases += 1;
        }
      }
      let percentage = ((sameBases / totalBases) * 100).toFixed();
      console.log(`Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${percentage}% DNA in common.`);
    }
  }
}
let starterDna = mockUpStrand();
let secondaryDna = mockUpStrand();
let pAequorComparison = pAequorFactory(1, starterDna);
let pAequor = pAequorFactory(2, secondaryDna);
console.log(pAequorFactory(1, starterDna).compareDNA(pAequor));

And then it shows something like this:
[ ‘T’, ‘T’, ‘A’, ‘C’, ‘G’, ‘C’, ‘G’, ‘C’, ‘C’, ‘A’, ‘C’, ‘G’, ‘C’, ‘C’, ‘A’ ]
[ ‘C’, ‘G’, ‘C’, ‘G’, ‘A’, ‘G’, ‘T’, ‘C’, ‘T’, ‘T’, ‘C’, ‘C’, ‘A’, ‘A’, ‘C’ ]
Specimen 1 and specimen 2 have 7% DNA in common.
undefined

Can anyone see what I did wrong?

the extra undefined is because the effect of your code is something like:

console.log( console.log(`Specimen ${this.specimenNum} and specimen ${pAequor.specimenNum} have ${percentage}% DNA in common.`) )

the function compareDNA already pints stuff to the screen (logs stuff to the console) because console.log() is inside that function
so you don’t need to do console.log again when you call the function:

console.log(pAequorFactory(1, starterDna).compareDNA(pAequor));

should just be:

pAequorFactory(1, starterDna).compareDNA(pAequor);

The undefined was because it was trying to console.log() something that doesn’t exist;
meaning it’s trying to console.log() the result of the function call compareDNA(pAequor)
but that function doesn’t return anything, so it just gives undefined.

[ A different alternative would be to have the function compareDNA return something,
for example: return percentage; ]

2 Likes

It worked! I just removed the second console.log() and it fixed it. Thank you!