Mysterious Organism help

// 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 = (num, arr) => {
  return {
    specimenNum: num,
    dna: arr,
    mutate(){
      let pos = Math.floor(Math.random() * 15);
      let n = this.dna[pos];
      let x = '';
      do {
        x = returnRandBase();
      } while (n === x);
      if (n !== x) {
        this.dna.splice(pos, 1, x);
      }
      return this.dna;
    },
    compareDNA(obj) {
      let results = 0
      for (let i = 0; i < 15; i++) {
        if (this.dna[i] === obj.dna[i]) {
          results++;
        }
      }
      let percenti = results/obj.length * 100
      let percent = (results/obj.length * 100).toFixed(2);
      return `The two DNA strands are ${percent}% in common.`;
    },
  }
}



const strand1 = pAequorFactory([ 'A', 'T', 'G', 'C', 'T', 'C', 'G', 'T', 'A', 'T', 'A', 'A', 'C', 'C', 'G' ]);

const strand2 = pAequorFactory(2, [ 'A', 'T', 'A', 'C', 'C', 'G', 'A', 'C', 'A', 'G', 'A', 'G', 'C', 'C', 'G' ]);


console.log(strand1.compareDNA(strand2))

Hey so I am currently trying to compare the two strands in a method, I feel like I have created the right method, but for some reason my console is throwing up an error message that say

’ if (this.dna[i] === obj.dna[i]) {
^

TypeError: Cannot read property ‘0’ of undefined’

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

Im assuming it is not registering that ‘this.dna’ will be a future array but unsure how I can get around this or if it is something completely different! Any help appreciated.

Hello, @dearman94.

You’ve got a few issues here, but first thing is to debug the error you’ve mentioned. Using console.log() statements to observe what your code is doing, and comparing that to what you wanted or meant for it to do is key to debugging. For the line of code mentioned in the error message, either this.dna[i] is undefined or possibly obj.dna[i] is undefined. I would try this as a first step:

Also, I edited your post, so your code would be readable, and able to be copied, and run by others to help you. Please review How do I format code in my posts?

Thanks for editing much appreciated will have a read of that post you sent in.

Ahh I see i left the number out of strand 1 kicking myself for that one!

So I notice I left the number out of strand 1, so that error is no longer appearing now it is printing ‘The two DNA strands are NaN% in common.’ which suggests there is something wrong with either the for conditional statement of the equations to get the percent.

The thing I struggle with is knowing what to log to the console to find where the error is within functions, loops or conditions to be able to target a specific point of error, any advice?

1 Like

Generally you want to observe data going in to an operation/function, and what comes out. If any of those are not what you expected, you have to go back a step to where they came from, and do the same thing until you find where things start to deviate from what you intended. Looking at your compareDNA() function, I would try adding the following:

In order for percent to be assigned to NaN, one of the values in the equation must be non-numeric.

I see so you just console.log the points where the data could potentially be either entered wrong or passing on the wrong data.

I noticed that it was incrementing the results number which then led me to the percent equation, which the only thing that could of been wrong was what I was dividing the results number by obj.length which was logging undefined, so then I see it was because i left out the .dna in between.

Wow all of them hours for such simple mistakes that I couldn’t see in frustration hahaha

So I’m assuming that is the best way to break it down and see where the problems are?

2 Likes

Nice work!

Making observations of what the code is doing beats staring at it for hours looking for typos. Use the error message(s) as a starting point, and start printing out everything that goes in and what comes out of any operation that makes a change or assigns a value. Printing variables after they are assigned is a fairly common process used while writing the code in the first place to make sure that we are doing what we think we are doing. Getting in the habit of printing things out as you go, and only making small changes or additions to your code before pausing to see if it’s doing what you wanted it to will help prevent having to look through lots of code for a bug.

2 Likes

I think this is one of the best bits of advice I have received since starting, I have been getting very frustrated due to not being able to correct myself and not knowing where I go wrong.
I think this will be a method that will be a game changer when making projects in the future so thanks for that!

2 Likes

You’re most welcome! Happy coding!