In one of the challenges it asks to use a while loop to fill an array of descending numbers. I know I can just use an ascending loop and unshift but this was my initial plan and I’m getting errors I don’t understand.
// Setup
var myArray = [x];
// Only change code below this line
var b = 6;
while(b > 0){
myArray.push(b);
b--;
console.log(myArray[b]);
}
Output:
undefined
undefined
undefined
4
5
6
Like I understand if I get the wrong range of numbers or one of them is undefined because while loops do an extra iteration but why so many undefined?
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
YES thank you so much. This helped me see it better. I was outputting the index not the array itself so of course the index of an array that it doesn’t have would be undefined.