extra output after loop - javascript

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.

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

How do I print While Loop?

Im trying to print the objects in my Array 'ana' but I keep getting a number 2 after the contents are printed. How do I remove the number 2?
var ana = ["I", "Love", "my mother"];
var a = 0;
mother = ana.length;
while(a < mother) {
console.log(ana[a]);
a++;
};
Logs:
I
Love
my mother
2
Don't worry. This is your interpreter behavior. Your code is not actually printing 2. You are seeing 2 because your javascript interpreter prints the value of last assigned value.
In your code, a++; executed in the last loop will return 2(because x++ returns x and then increments it) in the last loop thereby the return value of your whole expression is 2.
You must be printing 2 somewhere else after this loop, your code will only print 'I Love my mother'.. also, there shouldn't be any ; at the end of while loop.
See the demo here
You can move the a++ inside console.log to stop showing it. Like this
console.log(ana[a++]);
The value 2 was getting printed as the return value of your code, which is the last value that a gets.

Java script while loop stange behaviour in chrome console

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.

Why the eval() is not working with array

What's wrong with this code? Can anyone help out?
var read=new Array("i=10","j=20","k=i*j");
for(var i=0;i<read.length;i++)
{
alert(eval(read[i]));
}
Expecting output:
alert three times with values 10,20,200.
But actual output:
But alert Once with value 10.
When the loop executes the first time, you are setting i = 10, with eval. So the loop breaks out immediately.
So, you might want to change the loop variable to something else, like this
var read = new Array("i=10","j=20","k=i*j");
for(var idx=0; idx < read.length; idx++)
{
console.log(eval(read[idx]));
}
Output
10
20
200
Note: Please make sure that you read this question and the answers to that question, before using eval in your code.
Try this code
var read=new Array("i=10","j=20","k=i*j");
for(var index=0;index<read.length;index++)
{
alert(eval(read[index]));
}
while the loop is executing, at the first execution of i=10 the i variable is set to 10; so loop will be terminated because of the condition i<read.length (here... 10<3) remains false.
see the eval() tutorial.

How do I use shift inside a loop?

When I run this code:
var a = ['a','b','c'];
var b = ['a','b','c'];
for(i = 0; i <= a.length-1; i++){
b.shift();
console.log(b);
}
I expected this output:
['b','c']
['c']
[]
But I get this output:
[]
[]
[]
Why?
And how do I get my expected output?
This is a known problem in Chrome. It's because the console.log doesn't make a copy of what you want to display, it just stores the reference.
As the log isn't updated immediately, but once your function ends and the browser updates the user interface, the log will show the current state of the b variable, not the state when each console.log call was made.
To get the desired ouput you would have to make a flash copy of the state of the variable for each console.log call:
console.log(b.toString());
This is a side-effect of the console.log statements not being printed as the statements are executed. Note that if you replace your console.log with alert, the code works as expected.
Or change the log paramater to be an expression as per my answer to Javascript Funky array mishap
console.log ('' + b);

Categories

Resources