Reputation: 1063
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.
Upvotes: 0
Views: 152
Reputation: 33637
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.
Upvotes: 2
Reputation: 1063
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;
}
}
Upvotes: 0
Reputation: 166071
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();
}
Upvotes: 2