Syntax Error that I just can't figure out

I’m doing the Credit Card Checker exercise in JavaScript and I’m stumped on why a syntax error is returning. Please help! The link to the exercise is link The code is:

const validateCred = array => {
  for (var i = array.length - 2; i >= 0; i -= 2); {
  for (var array[i] = (array[i] * 2 % 9));
  }
    if (array[i] > 9) {
    return array[i] - 9
  } 
   else if (array[i] = array[i]) {
    return array[i]
  } 
};

And the syntax error is:

for (var array[i] = (array[i] * 2 % 9));
** ^**
SyntaxError: Unexpected token [

I would play around with indentation to clarify the intent of your statements.

You’re missing an opening bracket for the second for-loop, this will cause issues.

Once you’ve sorted the formatting and the bracketing, look out for punctuation (semicolons) and semantics (for-loop vs. conditionals).

It’s short enough you can re-write it from scratch, and then compile every time you finish a new expression to make sure that it doesn’t crash. Whenever you add for-loops on top of each other it always adds complexity so you want to make sure the foundation is sturdy.

1 Like

Thanks @toastedpitabread! What do you mean by

Hi again @toastedpitabread and any others! I’ve made some changes but the same syntax error is occurring.


const validateCred = array => {
  for (var i = array.length - 2; i >= 0; i -= 2) 
  {
  for (var array[i] = (array[i] * 2)
  {
    if (array[i] > 9) {
    return array[i] - 9
  } 
    else {
    return array[i]
   }
  };
  }; 
};













Hello! The syntax for a for loop looks like this:

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

The syntax for an if…else statement looks like this:

if (condition1 === condition2){
//code
}

What does this piece of code look like, and if…else, or for loop:

2 Likes

Hi @codeneutrino! Well, I want it to be a for loop so that I can create array[i] as a variable and then execute the code below it. Here’s my updated code:



const validateCred = array => {
  for (var i = array.length - 2; i >= 0; i -= 2) 
  {
  for (var array[i] = (array[i] * 2))
  {
    if (array[i] > 9) {
    return array[i] - 9
  } 
    else {
    return array[i]
   }
  };
  }; 
};













Like @codeneutrino mentioned, you should consider the formulation of your for-loop. You can read more about them in MDN documentation: for - JavaScript | MDN

because this line: for (var array[i] = (array[i] * 2)) is neither following a correct for-loop syntax or conditional syntax.

2 Likes

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.