Programing question

Hi,
i have a question about the difference between two codes i get different answer for them and don’t understand why.
1.

print = "#";
for (var i =0; i<7; i++){
  print += "#";
  console.log(print)
}
print = "#";
for (var i =0; i<7; i++){
  console.log(print);
  print += "#";
}

thanks,

I am not familiar with javascript at all but I have a general knowledge about programming from other languages.

Without knowing what different result you have gotten I think the different result is because in the first code block your for loop first add something to your variable “print” before writing it out on the console. The second code block you instead first write it out on the console log and then after adding something to the variable “print”.

So its all in the for loop what you start doing first in writing the code out in console or adding something to the variable.

Hope it helps and tell me if something is unclear :slightly_smiling_face:

3 Likes

Hi @BitbyBit,
Javascript is a top to bottom flow language. So which statement you write first will get executed first.

In the two examples given, the order of console.log(print); and print += "#"; is inter changed. Thus, you see two different results.

Like @FredrikLarsson explained, the variable “print” changes based on it.

Hope this helps.

Happy Learning! :slight_smile:

8 Likes

Thank you very much I got it. :grinning:

1 Like