Break away from while loop detecting an empty prompt string

Viewed 161

I have a tiny and simple JavaScript greeting script here

My goal is to detect if the user has submitted an empty string in the prompt box, then continue to prompt until the user finally gives in and types something in

The issue is, it correctly detects and empty string, but even when you type something in it still exectutes the while loop to continue prompting, therefore the code gets stuck in an infinit loop of Promt boxes

How would I break away from the while loop once the user has given a value?

    var Name = prompt("Insert Name");

while(Name === ""){
prompt("Empty")
}

document.write("Hello " + Name)
1 Answers

You only assign Name once, before the loop. You don't re-assign it inside the loop and hence the loop will run forever.

Try this:

var Name = prompt("Insert Name");

while(Name === ""){
   Name = prompt("Empty")
}

document.write("Hello " + Name)
Related