Pls can someone explain what is going on here

var printNumTwo;
for (var i = 0; i < 3; i++) {
  if (i === 2) {
    printNumTwo = function() {
      return i;
    };
  }
}
console.log(printNumTwo());

So, you first start out by creating a variable called printNumTwo.
The for loop will start at 0 which is i=0 and then evertime the loop completes i will add one to itself
then you are checking if i is the same number as 2
if that is correct then you assign the variable printNumTwo a function that returns i which in this case will be the number 3

Finally, you are logging that to the console to show the value that printNumTwo is holding

Wow! your explanation is so nice but what I don’t understand is why is it returning 3 instead if 2.

It can be confusing. So, right now the way the loop is set up it will loop 3 times before returning anything. So after every loop it adds one to itself, and right now there is nothing stopping the loop at the second loop. All the code is saying is on the second loop make a function that returns the i value. Because it loops three times the value will always be three

you could do something like this

var printNumTwo;
for (var i = 0; i < 3; i++) {
  if (i === 2) {
  printNumTwo = i;
  }
}
console.log(printNumTwo);

This says that once the i value gets to 2
store that value in the variable you created.
The loop will continue to lopp for a third time, but it will not change the variable value because we told the code to make that second loop our value

Once you get further in javascript you will see that returns can stop a loop, but that happens when a return is inside a function. Sort of what you originally had, but the loop also needs to be inside a function like this

var printNumTwo = function() {
for (var i = 0; i < 3; i++) {
  if (i === 2) {
        return i;
    }
  }
}
console.log(printNumTwo());

Once i ===2 here the loop will stop dead in its track and not loop a third time because of that return statement

Part of what you are seeing is that var is a bit wonky…

var printNumTwo;
for (var i = 0; i < 3; i++) {
  if (i === 2) {
    printNumTwo = function() {
      return i;
    };
  }
}
console.log(printNumTwo());

In this code, i is reused on every single loop iteration, so the i in the definition of printNumTwo is the same i that has its value increased.

But if we make a small change:

var printNumTwo;
for (let i = 0; i < 3; i++) {
  if (i === 2) {
    printNumTwo = function() {
      return i;
    };
  }
}
console.log(printNumTwo());

now each loop iteration has ‘its own’ i, so the value of i for the definition of printNumTwo is not changed.

Wow! Your explanations had been really helpful. Thanks so much for taking your time .

Maybe we could be friends so you help me out when I encounter problems again
I am actually new to JavaScript and I hope to be true with all freeCodeCamp challenges in three months from now.

Is there a medium we could converse with?

This forum and the freeCodeCamp chat platforms are pretty good for talking about learning programming :slight_smile:

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