# 'for' clarification needed

Tell us what’s happening:

Hello. Could someone explain why we have declared ‘i’ as a variable but we aren’t using it anywhere in the code? How is it connected to the str + “is cool!”?

Also, could you explain that code as well "(let i = 0; i < str.length; i+=2) ". I’ve been through the lesson but some things aren’t very clear for me.

So, basically i = 0 means that the process starts from zero? i < str.length = i < 12 in this case? i+=2 means we are adding ‘2’ on each cycle, but why do we only have 6x " freecodecamp is cool!" sentences? (taken from the console in the challenge).

Thanks in advance. Expect such questions from me in the future!

function printManyTimes(str) {

// Only change code below this line

const sentence = str + " is cool!";
for (let i = 0; i < str.length; i+=2) {
console.log(sentence);

}

// Only change code above this line

}
printManyTimes("freeCodeCamp");

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Declare a Read-Only Variable with the const Keyword

That’s how the loop works, you tell a condition in here ( ). When the condition is false, it stops running.

You can make a map to follow it:

let i = 0; i < str.length; i+=2

Cycle 1: i=0, is it smaller than str.length? then add 2

Cycle2: i=2 is it smaller than str.length? then add 2

At some point is false, and that stops the loop.

1 Like

Q: Could you explain that code as well "(let i = 0; i < str.length; i+=2) ". I’ve been through the lesson, but some things aren’t very clear for me

A: So, here is the syntax:

for (let i = 0; i < str.length; i+=2)

let i = 0 is declaring variable i, with the initial value is 0. See the difference between var, let, and const.

If let i = 0 is for the start line, then the i < str.length is for the finish line. It tells the computer that if the condition is fulfilled, then this loop should end.

Last, i += 2 is for how the iteration “walk”. In this case, it tells the computer to jump 2 numbers for every iteration. So, the initial value is 0 (from let i = 0) and the finish value is str.length (which is the amount of “freeCodeCamp” characters, 12), so when for-loop is executed, the i value should be:

[0, 2, 4, 6, 8, 10]

NOTE: 12 is not included because the condition is i < str.length, not i <= str.length. So that’s why we only have 6 result, because i = 12 is not included.

In summary, for loop’s structure is:

for (startingPoint, endPoint, howCodeShouldGo) {
...
}

Q: Could someone explain why we have declared ‘i’ as a variable but we aren’t using it anywhere in the code?

A: In for loop, you can use any variable you want (a, b, c, word, myName). Anything as long as it fulfills the variable’s name rules in (in this case) JavaScript. But why use i? It’s just one of the most commonly used characters among programmers.

Why i is not used anywhere else? because the i variable’s function is just to store the iteration (loop) sequence number and the value is always changing (e.g. at the beginning of loop i = 0, after first loop id successful i = 1, and so on).

2 Likes