the position of the while loop certainly will matter, with the two main areas of focus being
- What conditional check you use in your while loop, as you saw here using the 'i' from the for loop
- How you intend to escape the while loop, as the 3 examples show, each one displays a different output
You can certainly place the while on the outside, give it a try and see ) ; the for will then run until it's own condition becomes false or you 'break' out of it. No different to the while in this respect. Just keep in mind if you work with the variable i, then scope really will matter as you will need to declare i outside of while, and declare a different variable inside the for... don't forget find a way to break out of both loops. hopefully the following helps.
var counter = 0
var loop = function()
var i =0;
while (i < 3)
for(var j = i; j < 3; j++)
console.log("J is:" + j);
i++;// this will be different to i outside the for uncomment to test differences
//i++; //swap comments out to see the difference to the i inside the for loop
With that in mind then, the scope essentially means what parts of the code can see what variables in this case. As you see when using i, how you change i can affect how others see it.
So if i increments in while, then it goes back to the for it stays incremented, and so the for never runs more than once, because i == 10 and that is the conditional check for i.
Same goes with the new example, increment i in the for gives much different results then i outside the for, but still inside the while.
On to the examples/edits
1) It depends what you need for how they will work for you, you need to judge the requirement of your needs and compare them to the results you get, it is possible neither if these may meet your need but hopefully with your new found understanding you will be able to get there
2) Rather then give the answer just yet, you seem certainly competent, why not console.log() both the i and j and see if you can work out why, if not post back and someone will try to explain as soon as they can
3) There are numerous ways to control your loops, you can make a condition false, you can break out or you can continue inside them. Each has it's own merit and use and you would want to spend time researching them to decide but to not get too bogged down we'll look at just the break a minute: A break statement escapes/breaks out of it's loop and continues executing any code after the loop again if you console.log() the results this should help give a visual on what happens, you should notice how the variable i isn't being incremented, or j or whatever letter you choose