8.When to 'while' and when to 'for'..please help me with the output!


#1

hi all
This might be a silly question but please help me..
my code is
var count = 0;
var cities = ["ajmer","barmer","chandigarh","Delhi"]
for (var i = 0;i< cities.length;i++)
{
console.log("I have been to" + " " + cities[i]);
}
while(count<3)
{
console.log("i am looping");
count++;
}
the output is
I have been to ajmer
I have been to barmer
I have been to chandigarh
I have been to Delhi
i am looping
i am looping
i am looping
2

Can someone please explain why 2 is printed at the end!!


#2

Just ran the code, logs without the 2 coming up, I believe its something CA console related :slight_smile:

Are you still able to pass?


#3

yes i am able to pass but the output 2 is still there!!


#4

@benjnev thanks for precious time and support:slight_smile: