Tell us what’s happening:
I don’t understand the method of declaring a function inside a for loop like this. So far FCC has only taught you one method of declaring a fucntion is:
function abcxyz() {
…code…
}
Now suddenly in this example they declare a function like this:
abc = function() {
… code …
}
And at the end they call the function: abc();
I just don’t get it.
Your code so far
var printNumTwo;
for (var i = 0; i < 3; i++) {
if(i === 2){
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
// returns 3
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 OPR/62.0.3331.116
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/compare-scopes-of-the-var-and-let-keywords