Java script while loop stange behaviour in chrome console - javascript

Why does this:
i=0;
while(i<=4){
console.log(i);
i = i+1;
}
outputs: 0,1,2,3,4,5 but it should stop on the 5-th( 0,1,2,3,4 ) element not 6th(5)
PS: this happens if you paste the above code in google chrome console

The 5 isn't in logged in the loop, but its the i variable.
i=0;
while(i<=4){
console.log("log: " + i);
i = i+1;
}

This is the normal behavior under Chrome. It displays the last line evaluated. For instance, if you just enter: i=5; it will display 5 even though you didn't ask to display it through console.log. In your example the last line evaluated is i=i+1; which, at this point, computes the 5 value.

Related

Can anyone tell me that why the {For} Loop in JavaScript is returning all the values including the very first element of the an array?

var arr = [1,2,3,4,5]
for (var i=0; i<arr.length; i++){
console.log(arr[i]);
}
I ran this code on google chrome console and the output was like this:
1
2
3
4
5
In this code I can see that all the elements are been printed but the very first element of the array which is 1 should not print because the value of [i] has already been incremented to 1 and so 2 should be printed instead of 1.
Can someone tell me why is this happening ?
I think you are getting it wrong here. In for loop, we have
for(initialization, condition, change) {
// body
}
So, it workes in this sequence
initialization -> condition -> body -> change
PS: It will go to body if condition evaluates to true

extra output after loop

I am learning JavaScript on Codecademy. I am finding so many outputs like below when I use while loop.
for (var i=0; i<5; i++){
console.log("Dhanu identify problems and solve them");
}
var loop = 0;
while(loop<5){
console.log("Do some more");
loop++;
}
I am getting an unwanted output in the last line as 4.
The output you get is simply console.log which logs the last known value of the 'loop' variable: (here in firefox):
It's absolutely not related to your code and you don't have to worry about that.
As stated in another answer, debug console use to log the result of the last line (probably because they are meant to debug).
For example the statement "i = 1" evaluates to "1":
while the statement "var i = 1;" evaluates to "undefined", hence logging "undefined"
you can observe the same behavior by invoking eval on those statements:
You're seeing that because the last statement in your while loop is loop++, which "returns" the current value of loop. This is just a side effect of running code in a console like this. Consoles are basically read-execute-print-loops (REPL), and so they need something to print. If you ran this as a script in a webpage you would not see 4
when you write any statement in developer console then last returned value will be printed...
for example
var a;
it prints undefined on console.
a = 10;
it prints 10 on console.
a = 10; a = a + 5;
it prints 15 on console.
simply when we assign any values or increment or decrement numbers then it returns value after performing that operation...
so, in the above or below code
var loop = 0;
while(loop<5){
console.log("Do some more");
loop++;
}
finally loop++ is 4.

I don't understand a simple JavaScript 'while loop'

I'm currently learning JavaScript on Code Academy and I am at the end of the while loops section.
The following code:
j = 0;
while (j < 3) {
j++;
}
produces a 2 on the console and I have no clue why. I tried running this on Eclipse JaveEE only to realize that using a HTML file with this code as a script gives me a different result: a blank page.
That makes sense to me, because I've only incremented j to 3, but not printed it. Not sure why the CodeAcademy console gives me a 2.
This is a screenshot of the console output:
The behaviour you're observing is because that's how a browser console works.
For every code you evaluate it tries to return some value. For trivial expressions it's easy - 2 + 2 would presumably return 4.
For code that consists of multiple statements it's much more complicated and console tries to be smart. What adds more complexity into this is the fact that console's behaviour is not standardised, so what we observe at this very moment for a given browser is not guaranteed to hold true for another browser or for another release of the same one.
Let's try to find out what is happening though:
j = 0;
while (j < 3) {
j++;
}
for this code browser tries to be smart and outputs the value of the latest found expression, which is in this case is j++;. It returns 2 because that was the value of j on the last iteration before loop termination. And since the postfix increment returns the current value before modifying it - it returns 2.
If we change it to
j = 0;
while (j < 3) {
++j;
}
the output would be 3, for the very same reason.
Now let's try something different:
j = 0;
while (j < 3) {
j++;
a = 42;
}
this would output 42. Since the a = 42 is the latest expression in this code.
j = 0;
while (j < 3) {
j++;
var a = 42;
}
For this sample it would again return 2, since console decides to ignore the assignment statement and reverts back to the latest expression.
To summarise: this behaviour is not standardised, and browsers just try to be useful and to output something, even if it's not what you expect to be. So my advice would be to not rely on the implicit console output and use the console.log() explicitly in case when you want to get a result.

Firebug is printing one addtional numbers when executing code in its command line

I was executing JavaScript within Firebug's console. While running the below loop I have noticed it's printing one additional number:
var i = 0;
while(i<10) {
console.log(i);
i++;
}
It's providing the following output:
var i = 0; while(i<10) { console.log(i); i++; }
0
1
2
3
4
5
6
7
8
9
9
Is this a bug or something else?
The Console panel outputs the return values of scripts executed through the Command Line or the Command Editor. Unfortunately Firebug does not visually differentiate the return value from the output created through console.log(). So the second 9 at the end of the output is the return value.
Within the Firefox built-in DevTools this is more obvious:
Note that the output of the return value cannot be suppressed.
The last 9, which is causing disturbance, is the return value of console.log(). Try alert(i) instead of console.log(i).

javascript for loop not behaving as expected

I know this is really basic but I am new to Javascript
This is a piece of code from a battleship game I am working on for a class
function displayMyBoats(ship){
for (var i = 0; i<ship.length; i++)
{
alert("ship.length = " + ship.length);
alert("Ship[i]= " + ship[i]);
document.getElementById( "Set_" + ship[i] ).src = fnImageShip;
alert("i = " + i);
}
}
I am testing with a ship array that is 5 elements. Everything works fine until it reaches 5, then all of a sudden it thinks the length of the array is 8.
There is no code to increase the length of ship array, so what would cause it to add to the length of the array?
The alerts are my testing to see what all the values are at.
To give you a hint, try adding an alert whenever your script is either entering or exiting the displayMyBoats(). Suddenly jumping to 8 indicates that it's entering the method a second time, this time with a ship of len 8.
Confirm or deny this theory by adding this alert after your for loop block:
alert('exiting displayMyBoats()');`
Perhaps you see more if you use console.log(ship); instead of alert(); - if you are using Firebug/Console.

Categories

Resources