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>
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Anna Bannanna
  • 135
  • 3
  • 10

4 Answers4

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?
Mohit Bhardwaj
  • 9,650
  • 3
  • 37
  • 64
  • I would just use in `if` instead of a while. In the `else` block I would have `document.write("GTFO then.")` – Geeky Guy Jun 08 '16 at 16:58
  • @Renan you would use a function to call if the name is empty repeatedly then? – Mohit Bhardwaj Jun 08 '16 at 17:02
  • Thank you so much! That was exactly what I wanted to do! – Anna Bannanna Jun 08 '16 at 17:02
  • @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. – Mohit Bhardwaj Jun 08 '16 at 17:07
  • @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 – mirzhal May 23 '19 at 06:40
  • @mirzhal did you remove this line also - `name = prompt("What's your name?");` ? – Mohit Bhardwaj May 23 '19 at 09:20
  • @MohitBhardwaj, no, only removed name=, that is, I have - prompt("What's your name?") just why need to add "name =" before prompt. I hope you got my point)) – mirzhal May 23 '19 at 09:35
  • @mirzhal yeah that explains it. You are asking the user for a name, but not storing his input anywhere. So, the `name` variable is still empty, when the while condition is checked again and so it goes in the loop again and it keeps looping. Does that make sense? – Mohit Bhardwaj May 23 '19 at 09:48
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.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
  • Will have that in mind, but I'm currently learning how to use those methods! Learning how to code chapter by chapter. – Anna Bannanna Jun 08 '16 at 17:05
  • @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](http://stackoverflow.com/questions/11729835/how-to-disable-prevent-this-page-from-creating-additional-dialogs). There are other ways to force a user to give meaningful input to your page. – Geeky Guy Jun 08 '16 at 17:08
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>");
0

Use This

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