JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Create a `do...while` loop

professionalb
professionalb
Full Stack JavaScript Techdegree Student 5,082 Points

do...while - why would I want to rewrite this question?

I'm on the challenge asking us to rewrite a while loop into a do ... while loop.

I got the solution, but I still don't understand why I would use a do...while loop in this situation.

Given the existing code, the first thing we are doing is setting up a variable and prompting the user to enter a password. Then we are in the while loop as long as the entered password doesn't match the stored password.

There is no possible way to not get into the while loop because at load, the password is blank and therefore always meets the condition of the while loop (it does not equal the correct password).

If the whole point of a do... while loop is to make sure the while loop executes at least once, why would we use it in this scenario. In this case the while loop will always run regardless.

3 Answers

professionalb
professionalb
Full Stack JavaScript Techdegree Student 5,082 Points

Thanks!

I understand what a for...while loop does. I was just commenting that in this example, it doesn't make a lot of sense to use it. Even if the goal was to make the code more "DRY", there are better ways to do it.

Also, I've heard the suggestion that it's better to use MDN as a resource as it a respected/peer reviewed/ professional reference, while w3schools can be a bit "iffy" (even though most of the stuff on there is right).

MDN is cool, but in my opnion it takes time to get used to their way of teaching