1

I want to create a while loop to avoid empty input. Here's my code, I want it to loop so that the user gets the same alert and then prompt window until he/she writes a name/username. Any ideas on what I'm doing wrong and how to fix it?

<script type="text/javascript">
confirm("Wanna play?");
var name = prompt("What's your name?");
while (name.length == 0) {
    alert("Please enter your name!");
    prompt("What's your name?");
}
else {
    document.write("Welcome to my game " + name + "!" + "<br>");
}
</script>
0

4 Answers 4

4

@Renan suggested it right. Still, if you need the current code to work, you can try this:

<script type="text/javascript">
confirm("Wanna play?");
var name = prompt("What's your name?");
while (name.length == 0) {
    alert("Please enter your name!");
    name = prompt("What's your name?");
}
document.write("Welcome to my game " + name + "!" + "<br>");
</script>

There are some errors in your code/logic:

  1. Why is there else after while? else is used with if clause only.
  2. You are asking for the name in the while loop, but not assigning it to the name variable. If you do not assign to name, how would the name variable get updated and cause the while loop to exit?
Sign up to request clarification or add additional context in comments.

8 Comments

I would just use in if instead of a while. In the else block I would have document.write("GTFO then.")
@Renan you would use a function to call if the name is empty repeatedly then?
Thank you so much! That was exactly what I wanted to do!
@Renan ok. But OP probably wants to create a demo for something where he needs the user to enter the name first, only then the flow should proceed.
@MohitBhardwaj, hi Mohit, I am beginner in Javascript and would like to ask you, I wrote: while (name.length == 0) { alert("Please enter your name!"); prompt("What's your name?"); } and removed variable name from prompt but alert("Please enter your name!"); keeps iterating. Why is that? Please help
|
3

Never abuse prompt, alert and confirm in your pages.

I could write a lot about this but I will just leave this image here.

Users can easily silence your attempts to call their attention via dialogs, so any extra effort you spend on that is wasted.

2 Comments

Will have that in mind, but I'm currently learning how to use those methods! Learning how to code chapter by chapter.
@AnnaBannanna it's ok, I see a lot of experienced programmers that don't know about this either. This might be a bit much for now if you are just beginning, but you might like to look at this other question. There are other ways to force a user to give meaningful input to your page.
0

See If this does what you're looking for!

confirm("Wanna play?");
var name = prompt("What's your name?");
while (name.length == 0) {
    name = prompt("Please, insert a name to proceed!");
}
document.write("Welcome to my game " + name + "!" + "<br>");

Comments

0

Use This

var name = prompt("What's your name?");
while (name.length == 0 || name == "null" || name == null) {
    name = prompt("What's your name");
}

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.