FAQ: Loops - The For Loop

Why is this not an accepted answer?

// Write your code below
for (i = 5; i <= 10; i++){
  console.log(i)
}

2 Likes

that seems correct, and when i run your code in the exercise it get green light

Nope not for me… restart the exercise and try it again, it will pass after using the original solution proved by codeacademy, and it will then accept this version above.

I just did it again, it fails.

35%20PM

2 Likes

It may be expecting let i = 5 in the loop parameters.

3 Likes

Bingo! That was it! Thank you mtf!

1 Like

5 posts were split to a new topic: ‘function’ instead of ‘for’

Just a quick and possibly a silly question but what is the difference between using an IF statement vs the For Loop, they both sound a little similar.

1 Like

if checks a condition, while for loop allows us to loop over array, strings and more:

let x = 3;
if (x < 5){
   console.log("3 is smaller then 5");
}

const myArray = [1, 2, 3, 4, 5, 6, 7, 8]
for (let i = 0; i < myArray.length; i++)
{
    console.log(i);
}

we can even combine loop and if, for example to check all the values in the list:

const myArray = [1, 2, 3, 4, 5, 6, 7, 8]
for (let i = 0; i < myArray.length; i++){
  if ( myArray[i] < 5 ){
    console.log(`${myArray[i]} is smaller then 5`);
  } else {
    console.log(`${myArray[i]} is NOT smaller then 5`);
  }
}
3 Likes

But is this “proper” syntax? Why are they instructing us to use ‘let’ when the code runs fine without it?

When starting with programming, its really difficult to grasp when multiple approaches seem to work, which one is “best” and why.

i would absolute recommend using let, which has block scope:

for (let i = 0; i < 5; i++){
   console.log(i);
}
// i variable only exist within the for loop, which we can see:
console.log(typeof i);

if we wouldn’t use let (and not even var), the variable would exist outside the needed scope:

for (i = 0; i < 5; i++){
   console.log(i);
}

console.log(i);

this pollutes the global namespace, more change of conflicting variable names and so forth.

6 Likes

This exercise is not functioning properly.
I entered:

for (let counter = 5; counter < 11; counter++) {
console.log(counter);
}
received an X.
Asked for the solution and was given:

for (let counter = 5; counter < 11; counter++) {
console.log(counter);
}

On LOOPS
The For Loop lesson it begins with this:

a stopping condition is the condition that the iterator variable is evaluated against— if the condition evaluates to true the code block will run, and if it evaluates to false the code will stop.

So, the condition that we use needs to always be true?

Correct. That is what controls the iterations. Once no longer true, iteration ceases.

At first I tried this approach to solve the exercise.
Why can’t the stopping condition equal to 10? It gives me an eternal loop.
But since it was a ++, it should go to 11 afterwards and stop, no?

for(let counter = 5; counter = 10; counter++)
{
console.log(counter);
}

here:

counter = 10

you don’t do a comparison, you do an assignment. After the assignment, JS will check if 10 is a truthy value (which it is), so now 10 is evaluated as truthy every iteration of the loop causing an infinity loop

1 Like

I don’t understand why the output starts with 0 with the following loop:

for (let counter = 0; counter < 4; counter++) { console.log(counter); }

I thought it would start with 1 since we console.log after counter++.

Hello, @katell2020.

Welcome to the forums.

The counter variable will be incremented by 1 after each iteration of the for loop’s code block then the stop condition is checked prior to the next iteration of the code block. In this case, your stop condition is: counter < 4;, so as long as that evaluates to ‘truthy’, the code inside the block will be executed again.

2 Likes

I understand that counter=10 will reassign and evaluate to truthy, but why does the following code not work?

for (let counter=5; counter===10; counter++) {
console.log(counter);
}

The condition of the loop is false (5 === 10), so the loop never runs.

The loops run while/as long as the condition is true

Ah! Thank you! I was thinking that it would run UNTIL the stopping condition is true. Thank you for clearing that up!