Hi @Ziheng,
I understand what you mean. In the code below ,
The Chrome console always tries to print the evaluation of a statement. In addition to the output of console.log()
calls, the console also displays the value of the last expression executed in the code you run there. Thus, the extra "########” is NOT from the console.log()
that you wrote. It is the general outcome of an expression. The expression, in this case, is the for loop, and the outcome is the variable “x”.
If you write the same thing in an actual script file and run it on the browser you won’t see such a behaviour.
Now, for the below code,
When you declare the pyramid variable all the way on the top, it becomes a global variable. That mean when the second loop i.e. for(var column = 0; column < row; column++)
finishes , the pyramid variable doesn’t get resetted. It carries the old value, now if you add the string, it carries the previous string value and adds the new loop string, that’s why you see unexpected long strings.
When you declare the pyramid variable just below for(var row = 0; row < 7; row++)
, it resets (technically re decalres ) itself when the second loop for(var column = 0; column < row; column++)
finishes. Thus, in this case, you see the expected answer.
This exercise really take a toll to your imagination. Feel free to play around and discover the intricacies of the language. You will have fun
Hope this clarifies your doubt.
Happy Learning!