I Can't See The Syntax Error!

Hi Everyone, I’ve been looking and looking at this and I’m no longer being productive. The code is throwing an syntax error on the line for (let percent = matchCount / 15 * 100) { saying:

SyntaxError: Unexpected token )

What am I not seeing?

      compareDNA(specimen) {
        let zip = [];
        let array1 = this.dna;
        let array2 = specimen.dna;
        let matchCount = 0
        for (let i = 0; i < array1.length; i++) {
          zip.push([array1[i], array2[i]]);
        } return zip;

       if (array1[i] === array2[i]) {
          matchCount++;
        }

        for (let percent = matchCount / 15 * 100) {
          percent.toFixed();
        } console.log('Specimen ' + this.specimenNum + ' and specimen ' + this.specimenNum ' have ' + percent + ' percent DNA in common.');
          }
        } 
      },


org1 = (pAequorFactory(1, mockUpStrand()));
org2 = (pAequorFactory(2, mockUpStrand()));
console.log(org1.compareDNA(org2));

The syntax for a for loop is:

for (let i = 0; i < condition; i++) {
//code
}

You can change the variable names, condition, variable values, etc, but the basic template must be the same. Compare the above to yours:

for (let percent = matchCount / 15 * 100) {
    //code
}

Does it seem the same?

1 Like

In addition to what @codeneutrino said, this return will stop your function all codes below it are unreachable.

2 Likes

Thanks @codeneutrino ! I shouldn’t have used a for loop. I’ve changed it to an ordinary variable now.

1 Like

Thank you @enghosamokasha ! I mistakenly left it in there after testing my code.

1 Like

Thanks both for your help. I’ve amended the code but there’s still a syntax error thrown:

      compareDNA(specimen) {
        let zip = [];
        let array1 = this.dna;
        let array2 = specimen.dna;
        let matchCount = 0
        for (let i = 0; i < array1.length; i++) {
          zip.push([array1[i], array2[i]]);
        } 

       if (array1[i] === array2[i]) {
          matchCount++;
        }

        let percent = (matchCount / 15) * 100;
          percent.toFixed();
          console.log('Specimen ' + this.specimenNum + ' and specimen ' + this.specimenNum ' have ' + percent + ' percent DNA in common.');
          }
        } 
      },


org1 = (pAequorFactory(1, mockUpStrand()));
org2 = (pAequorFactory(2, mockUpStrand()));
console.log(org1.compareDNA(org2));

What is the error? There seems to be a trailing } here (but it could just be the closing } of your function; I’d have to see the full code to know for sure):

let percent = (matchCount / 15) * 100;
          percent.toFixed();
          console.log('Specimen ' + this.specimenNum + ' and specimen ' + this.specimenNum ' have ' + percent + ' percent DNA in common.');
          }<----

and a trailing comma here:

let percent = (matchCount / 15) * 100;
          percent.toFixed();
          console.log('Specimen ' + this.specimenNum + ' and specimen ' + this.specimenNum ' have ' + percent + ' percent DNA in common.');
          }
        } 
      },<------
2 Likes

I think you missed a + before ' have ', also may we see the full code?

what mockUpStrand() returns? and what is the reason for these extra parentheses?

1 Like

Thank you so much all. I’ve edited my code with your suggestions. Still throwing errors though. Here is the full 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 : specimenNum,
      dna : dna,
      mutate() {
        let dnaM = this.dna[Math.floor(Math.random() * this.dna.length)];
        let randI = [Math.floor(Math.random() * 14)];
        this.dna.splice(randI, 1, dnaM);
        if (this.dna === mockUpStrand) {
          mutate()
        } else {
          return this.dna;
        }      
        },
      compareDNA(specimen) {
        let zip = [];
        let array1 = this.dna;
        let array2 = specimen.dna;
        let matchCount = 0
        for (let i = 0; i < array1.length; i++) {
          zip.push([array1[i], array2[i]]);
        } 
       if (array1[i] === array2[i]) {
          matchCount++;
        }

        let percent = (matchCount / 15) * 100;
          percent.toFixed();
          console.log('Specimen ' + this.specimenNum + ' and specimen ' + this.specimenNum + ' have ' + percent + ' percent DNA in common.');
          }
        } 
      },


org1 = (pAequorFactory(1, mockUpStrand()));
org2 = (pAequorFactory(2, mockUpStrand()));
console.log(org1.compareDNA(org2));

The error is now:

 if (array1[i] === array2[i]) {
              ^

ReferenceError: i is not defined

The error occurs because you are using i in array1[i] and array2[i] outside its scope. just push this if inside the for loop.

for (let i = 0; i < array1.length; i++) {
          zip.push([array1[i], array2[i]]);
          if (array1[i] === array2[i]) {
              matchCount++;
          }
} 
      

this will fix the error but, I’m not sure if this is the logic you want or not.

2 Likes

I think you forgot to fix the error that @codeneutrino mentioned. you have an extra comma.

2 Likes

Thank you! It’s not throwing any syntax errors now! It’s still not perfect but will work on it by myself for a while. Thanks again!

2 Likes

keep going, And don’t worry we all make mistakes just relax and think, and also you can use a pen and a paper to analyze your code and thinking before coding :boom:.

2 Likes

Thanks @enghosamokasha . It’s true! We learn through making mistakes so they are by no means a bad thing!

2 Likes