0

We're using a promo code to allow people access to a page. It doesn't have to be secure - just trying to give the customers a code prompt to make them feel special.

So I used this code... obviously I am doing something wrong because the browser keeps checking for the code even after they entered it (and eventually the page crashes). I tried break; at the end, but then prompt doesn't happen.

I would appreciate any help with this.

All I want to do is get rid of the endless loop here.

var secret;

while (secret !== "1")
  if (secret !== "2")
    if (secret !== "3")
      if (secret !== "4") {
        secret = prompt("What is the secret password?");
      };
Barmar
  • 741,623
  • 53
  • 500
  • 612
Cliff
  • 57
  • 7

2 Answers2

4

The problem is improper use of while mixed with if. Maybe you should consider an other approach:

var askSecret = function askSecret() {
    var secret = prompt("What is the secret password?");
    if(secret !== "theSecret") { askSecret(); }
};
    
askSecret();

Edit: multiple passwords/secrets

In the previous example, the only valid secret/password is "theSecret". If you need to have more than one secret/password, you'll have to extend the conditional with an AND && operator:

var askSecret = function askSecret() {
    var secret = prompt("What is the secret password?");
    if(
        secret !== "theSecret"
    &&  secret !== "TheOtherSecret"
    &&  secret !== "TheThirdValidSecret"
    )
    {
        askSecret();
    }
};
    
askSecret();
Booster2ooo
  • 1,373
  • 9
  • 11
  • thanks! I was trying to do this like the answer Barmar provided, but I derped. Please see my answer on Barmar's post. This also works but I was trying to allow more than one answer. I really appreciate your reply! – Cliff May 10 '17 at 17:25
  • Change `if(secret !== "theSecret")` by `if(secret !== "myFirstSecret" && secret !== "mySecondSecret")` – Booster2ooo May 12 '17 at 16:51
  • thanks, nice work. I was using || instead of && in some of my attempts – Cliff May 24 '17 at 15:46
1

Your code goes into an infinite loop if the user types any of the numbers in the if (secret != XX) lines. This is because the while condition is met, but the if condition fails, so it skips over the prompt() function and repeats the while. Since this doesn't change answer, it keeps doing this over and over.

If you want to check for multiple possible answers, do them all in the while statement:

var secret;
while (secret != "1" && secret != "2" && secret != "3" && secret != "4") {
  secret = prompt("What is the secret password");
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yo! This is what I was trying to do, but I used || instead of &&. Can you tell me why it it && and not || ? Thanks! – Cliff May 10 '17 at 17:21
  • @Cliff Read http://stackoverflow.com/questions/26337003/execute-block-if-a-variable-is-not-one-of-some-specific-values – Barmar May 10 '17 at 17:46
  • Just think about it logically. If you type 3, then you have `true || true || false || true`. Since `||` is true if any of its operands is true, this is true, so it keeps looping. – Barmar May 10 '17 at 17:49