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++) {
  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;
      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.

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; ]


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

Are you sure your percentages are right? By my count , using the example given above, you have 2 base pairs (4 same bases similar total in common which means that you have 4 bases similar/30 total bases ? 4/30*100 = 13%, not 7% as your code calls. Correct me if I’m wrong because I’m having similar issues.

You’re right. Lesson to me to double-check my math before I call a project done. Guess I’ll have to do the lesson again and find out where I went wrong.

I think I found the problem, but my computer and Internet are acting up and I’m having trouble running it to check. I’m pretty sure I had the compare DNA step right, but I put in the wrong numbers in the WillLikelySurvive part here;

willLikelySurvive() {
      let cgBases = 0;
      for (let i = 0; i < this.dna.length; i++) {
        if (this.dna[i] === 'C' || this.dna[i] === 'G') {
      let percent = ((cgBases / 15) * 100);
      if (percent >= 60) {
        return true;
      } else {
        return false;

This line specifically:

let percent = ((cgBases / 15) * 100);

I should be dividing cgBases by 30, the number of samples, not 15, then multiplying by 100. I’m pretty sure that’s where I went wrong based on your message and going back over the lesson, but like I said, my computer and Internet aren’t working right so I haven’t been able to double-check it anywhere.

1 Like