Reputation:
It happens in PHP as well. Whenever pass1 is entered to the prompt popup, the alert below it shows up as usual. But after that, the alert box of else also shows up. How can I stop the alert box of else from executing on pass1?
function download()
{
x=prompt("Enter the download code here.")
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
if (x=="pass2")
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
}
Upvotes: 0
Views: 113
Reputation:
Because you have used two if
statements but for your solution it needs to be single if
statement.
So just replacing your second if
statement with else if
.
e.g,
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2") // else if
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
Upvotes: 0
Reputation: 570
Because in your condition if (x=="pass1")
satisfied so it prompts "pass1",
Then as you have used one another if statement which is if (x=="pass2")
also get satisfied as this is different than your above if condition.
So its better to use if
and else if
for your condition.
Your code should be like this,
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2") // use of else if
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
Upvotes: 0
Reputation: 5126
Two things,
if
blocks execute if passed. When they fail, they try to find any associated else
block and execute. The if
context is lost after this point.
Your code basically says:
If x=='pass1' -> show Get the hell out of here. Else block is not present.
If x=='pass2' -> Tell him you like pie too. ( :/ ) Else -> Show msg that code is invalid.
So, basically when someone runs the code with pass1, they are told to get lost. THEN, another check is performed for pass2 and since this fails, they are shown the invalid code error.
Solution, use else if
statements as pointed in other solutions or even Better use switch case.
Upvotes: 0
Reputation: 38613
You need to use an else if
function download()
{
x=prompt("Enter the download code here.")
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2")
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
}
Upvotes: 0
Reputation: 28763
Try with else if
like
if (x=="pass1")
{
alert("This function has been deleted by the administrator. Jeff, get the hell out of here.")
}
else if (x=="pass2") // Here use else if
{
alert("I like pie too.")
}
else
{
alert("The code you specified was invalid.")
}
You can also use switch
like
switch(x) {
case "pass1" :
alert('This function has been deleted by the administrator. Jeff, get the hell out of here.');
break;
case "pass2" :
alert('I like pie too.');
break;
default :
alert('The code you specified was invalid.');
}
Upvotes: 2