For loop nested and the use of square brackets

Hi everyone,

I am doing practice exercises using “A Smarter Way to Learn Javascript” book and site.

Please explain to me why:

for (var i = 0; i < 5; i++) { 
  for (var j = 0; j < 5; j++) { 
    alert(i + j);
  }
}

and

for (var i = 0; i < 5; i++) { 
  for (var j = 0; j < 5; j++) { 
    alert([i] + [j]);
  }
}

Yield different results. The difference is in the use of square brackets in the alert statement.

P.S: I was able to understand the first example but not the second.

Good day i wish i could i am also stuck on the very first exercise on changing the element can you please help>

I am not speaking about the first exercise this is another issue. This is chapter 20 of "A Smarter Way to Learn Javascript” book.

Well, with for loops with more than two calls you always need brackets which is why the second is different. than the first. The second you are actually going through the loop as in 1, 2, 3, 4, etc. The first you are just incrementing it by one and since you don’t have brackets I am assuming it is not going through the actual loop. If that’s not it, let me know any more info on it and I will see how I can help.

This is the output of upper code :

0
1
2
3
4
1
2
3
4
5
2
3
4
5
6
3
4
5
6
7
4
5
6
7
8

This is the output of lower code :

00
01
02
03
04
10
11
12
13
14
20
21
22
23
24
30
31
32
33
34
40
41
42
43
44

upper code treat i & j as number
lower code treat i & j as string, (just like “a” + “b” = “ab”)

1 Like

^^^^ That could be it too but when I run the code it only alerts the number 1 where as the second will alert like a loop would.

Your explanation seems very logical. Thanks

Good day Sean can you please help me out on Comment out HTML i am stuck there?

Commenting out html is pretty easy you just use a it has two dashes on each side. Remember to put a ! on your first part or it wont work. You can do both single line and multi line like so. Hope it helps.

Wow my comment didn’t show up hahah. I forgot that did that in the forums. here go here: http://html.com/tags/comment-tag/

Just Google: “how to comment in html” and you will find the answer.

Most often to find answers you Google: How + your question.

Your question must comprise some keywords that you need to know. If you don’t know these keywords. Keep Googling and reading till you find them.

Good day Sean i hope you are good thanks today i tried to used your example but it did not take me to the next challenge.Please guide me via whatsapp here is my number +27799127492.Looking forwared to hear from you soon.