0

I am trying to get username for a site using the javascript prompt. If the user presses the cancel the prompt should come up again or if he enter empty value, the prompt should come up again.

I don't want the user to use the site without entering the proper username. for that I wrote a function to detect if username is entered or not. But it is not working correctly, it returns the value for the first time, but If i press cancel and enter the value in the second prompt. that function returns nothing. I don't understand what I did wrong.

function getusername()
{
  var user = prompt("Please Enter Your Username");
  if(user == null || user == "")
  {
    getusername();
  }
  else
  {
    return user;
  }
}

when I call this like alert(getusername()); the value comes in the first time, but if I press cancel in the first prompt and enter username in the second prompt, the alert is undefined. Please help me.

3 Answers 3

2

Others have mentioned you're missing a return in the case where you didn't get the input you wanted.

But more generally speaking, there's a problem with your method, in that it uses recursion unnecessarily. By calling getusername() from within itself, you wind up with a call stack than can grow as deep as the number of invalid inputs you get. In theory, if a user were to just keep hitting enter on an empty prompt then you could generate a stack overflow.

Sometimes recursion is a perfect fit for a problem, and if you don't use it you're going to wind up shooting yourself in the foot. But this is an example of a case where you're probably better suited with something like a while loop:

function getusername() {
   var user = null;
   while (!user) {
       user = prompt("Please Enter Your Username");
   }
   return user;
}

That can tolerate an infinite number of blank prompts, regardless of stack size. It would also be easier to have control over counting how many times invalid input were entered in order to impose a limit.

As a trivial function it probably doesn't really matter what you do. And it's important to keep your thinking fluid, as some languages actually force your hand to formulate such things recursively. It's just useful to know the tradeoffs. Yet I'd say in an imperative programming language like JavaScript, this is an instance where using recursion is a poor choice.

3
  • thank you. HostileFork, I learnt the new thing about recursion in javascript.
    – Vignesh
    Oct 29, 2014 at 13:09
  • 1
    @Vignesh I want to emphasize not to take away from this that "recursion is bad". On the contrary, it can be very good...and is a foundation of framing a problem in terms of smaller sub-problems in a divide-and-conquer way. But think about pausing in the debugger after 10 invalid prompt responses...do you want to look in the call stack and see the function that called getusername or getusername/getusername/getusername/getusername/getusername/getusername/getusername/getusername/getusername/getusername...? Oct 29, 2014 at 14:29
  • yeah I used recursion in embedded c it is very useful there. but I understand I didn't used recursion in its perfect use case in this code. thank you.
    – Vignesh
    Oct 30, 2014 at 11:48
2

Your if branch doesn't return anything. When a function doesn't return anything it implicitly returns undefined, hence your alert. You need to return from your if branch:

function getusername()
{
  var user = prompt("Please Enter Your Username");
  if(user == null || user == "")
  {
    return getusername();
  }
  else
  {
    return user;
  }
}

This makes the else redundant so you can shorten your code a bit (notice that I've also changed the condition to !user which should cover all eventualities here):

function getusername()
{
  var user = prompt("Please Enter Your Username");
  if(!user)
  {
    return getusername();
  }
  return user;
}

And that leads on to an ever shorter one-liner:

function getusername()
{
  return prompt("Please Enter Your Username") || getusername();
}
5
  • yeah. I just noticed that and corrected and post my own answer below. thank you anyway.
    – Vignesh
    Oct 29, 2014 at 12:02
  • ...@Vignesh and if you keep hitting enter at the prompt you eventually get a stack overflow. :-/ One level of stack depth added for each blank input? Oct 29, 2014 at 12:06
  • so is there any better way @HostileFork
    – Vignesh
    Oct 29, 2014 at 12:07
  • 3
    @Vignesh While loops? "while I haven't gathered a user name that is to my satisfaction, keep prompting"? var user = null; => while (!user) { user = prompt("Please Enter Your Username"; } => return user; Oct 29, 2014 at 12:09
  • @HostileFork your way is the right way, you should post this as an answer so I can accept it.
    – Vignesh
    Oct 29, 2014 at 12:25
0

I solved this by a small change,

function getusername()
{
  var user = prompt("Please Enter Your Username");
  if(user == null || user == "")
  {
    return getusername();
  }
  else
  {
    return user;
  }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.