Why the undefined in my while loop?


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/2/4?curriculum_id=506324b3a7dffd00020bf661


Hi, I'm not able to understand how the while loop worked in this code:


var nameArray = ["Maggie", "Tom", "Ritchie", "Emma", "Rupert"];

//While loop
var j = nameArray.length;
while(j > 0) {
    console.log("Hey! I'm in a while loop" + " " + nameArray[j]);
    j--;
}

The output was:

Hey! I'm in a while loop undefined
Hey! I'm in a while loop Rupert
Hey! I'm in a while loop Emma
Hey! I'm in a while loop Ritchie
Hey! I'm in a while loop Tom

Why is the first line "undefined". In this code, "j" is initialized to 4 which is the array length, so, while printing the name, why is it undefined?


#2

var j = nameArray.length; // var j=5

while(5 > 0) {
    console.log("Hey! I'm in a while loop" + " " + nameArray[5]);

nameArray[5] == undefined // Arrayindex starts from 0

#3

Hi rcodeman, thank you for pointing it out.

I have changed my code accordingly to this:

var nameArray = ["Maggie", "Tom", "Ritchie", "Emma", "Rupert"];

//While loop
var j = nameArray.length - 1;
while(j >= 0) {
console.log("Hey! I'm in a while loop" + " " + nameArray[j]);
j--;
}

output is as expected:
Hey! I'm in a while loop Rupert
Hey! I'm in a while loop Emma
Hey! I'm in a while loop Ritchie
Hey! I'm in a while loop Tom
Hey! I'm in a while loop Maggie


#4

As @rcodeman already mentioned the problem is that your array is out of boundaries. You start at 5 which is not an array index any more and you stop before 0 so you cut off "Maggie" (the first array index). And according to your second example you already fixed this.

So my question is rather: Why would you use a while loop when a for loop is much nicer in this situation:

for(var count = 0;count < array.length;count++)

or backwards:

for(var count = array.length-1;count >=0;count--)

Each one displays all the needed information in just one line instead of spreading them over the whole course of the loop. Don't get me wrong I'm not advocating against while loops but if you want to practice them it would make sense to practice them in situations where the use actually makes sense. For example while loops are awesome because they just have a condition, so in cases where you simply do not know when this loop might stop (for example dealing with randomness or user interaction) while loops are the feature of choice. When you clearly know a start and end condition before a loop starts then for loops can wrap up all the information in a pretty one-liner. And although do/while loops might seem hard to grasp at the beginning their purpose is crystal clear -> run the code once and the go on or enter a regular while loop.

So as an example of how a while loop approach to this problem might look like have a look at this:

var nameArray = ["Maggie", "Tom", "Ritchie", "Emma", "Rupert"];

var count = 0;

while(nameArray[count]){
    console.log(nameArray[count++]);
}

Here we use the fact that undefined has a falsy value, so the loop will stop when you exceed the boundary. And about this:

console.log(nameArray[count++]);

here I make use of the fact that variable++ has, as a statement, a value of variable and afterwards increases variable by 1. You can of course use variable here as well and then use variable++ on the next line, but then you might see an additional number in the output because as said the variable++ has a value and the last value is echoed when the program ends.

This is probably a little to complicated at this point and makes use of things that are not really covered in this course, so just take it as an idea of when to use a while loop. Yours works fine it's just that for counting backwards you already need to know the length and if you know the length a for loop is better.

Hope this helps and is not too confusing :slight_smile:

PS: It might not seem to be that different from your approach, but the lack of a .length also means that you could modify the length of that array on the fly and this loop would still work. (of course taken into account that the modification takes places at a point after the current index.)


#5

Hi haxor,

Thanks for explaining the concept of while loops so clearly.
In the tutorials, in the "When to for, when to loop" section, the application of 'while' and 'for' loops was clearly explained, same as you. Your explanation perfectly makes sense.

My "while" loop is just a 'disguised' "for" loop.
Silly me, when it comes to application, i overlooked this.
Thanks once again.

I will use while loop when the end point is unknown.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.