Why is it saying that `i` is not defined?

The following code is returning an error saying that i is not defined. But I have defined it!!

// Write your code here:

const greetAliens = arr => {
  for (let i = 0; i < arr.length; i++);
  console.log('Oh powerful ' + arr[i] + ', we humans offer our unconditional surrender!');

};
const aliens = ["Blorgous", "Glamyx", "Wegord", "SpaceKing"];

greetAliens(aliens);


Is the console.log actually in the for loop? The syntax for a for loop is:

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

Notice the curly braces ({})?

1 Like

His console.log is in the for loop

Blockquote[quote=“vickyrai, post:1, topic:563397”]

  for (let i = 0; i < arr.length; i++); {
  console.log('Oh powerful ' + arr[i] + ', we humans offer our unconditional surrender!');

};

[/quote]

1 Like

no curly brackets is allowed when dealing with a single line. I would still recommend curly brackets, but there is subtle difference.

the problem is the semi-colon at the end of your for loop:

for (let i = 0; i < arr.length; i++); <- this semi-colon

this semi-colon interrupts the for loop, resulting in the error.

2 Likes