2

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

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.