Javascript: Alert message after loop ends

for loop continue javascript
javascript break if
javascript break nested loop
javascript continue foreach
javascript for loop
while loop javascript
illegal continue statement: no surrounding iteration statement
javascript break foreach

This is a simple login excercise for school. It is meant to give you 3 attempts to log in. I would like to make it so after the loop stops (the three attempts were used), it alerts the user that he has no remaining attempts and his account will be blocked.

Something like:

alert("You don't have any attempts left. Your account is now blocked);

Here is the loop I made:

var tries;

for (tries = 2; tries !== -1; tries--) {
    let User = prompt("Enter your username:");
    let Pass = prompt("Enter your password:");
        if (User === "hello" && Pass === "world") {
            alert("Welcome.");
            break;
        } else {
            alert("Incorrect username and/or password. You have " + tries + " attempt(s) left.");
        }
}

Thanks in advance.

You where very close. I think this is what you want.

var tries;

for (tries = 2; tries >= 0; tries--) {
    let User = prompt("Enter your username:");
    let Pass = prompt("Enter your password:");
        if (User === "hello" && Pass === "world") {
            alert("Welcome.");
            break;
        } else if (tries == 0) {
            alert("You don't have any attempts left. Your account is now blocked");
        }  else {
            alert("Incorrect username and/or password. You have " + tries + " attempt(s) left.");
        }
}

JavaScript Message Box: popup, alert, confirm, Learn how to display popup messagebox in JavaScript using alert(), confirm() and prompt() functions. The setTimeout above schedules the next call right at the end of the current one (*).. The nested setTimeout is a more flexible method than setInterval.This way the next call may be scheduled differently, depending on the results of the current one.

You can achieve this recursively. Just decrease the number of Tries everytime wrong username or password is entered.

var TRIES = 3;

function ask() {
  let User = prompt("Enter your username:");
  let Pass = prompt("Enter your password:");

  if (User === "hello" && Pass === "world") {
    return alert("Welcome.");
  }

  if (TRIES > 0) {
    alert("Incorrect username and/or password. You have " + TRIES + " attempt(s) left.");
    TRIES -= 1;
    ask()
  } else {
    alert("You don't have any attempts left. Your account is now blocked");
  }
}

ask()

DOM Scripting: Web Design with JavaScript and the Document Object , Web Design with JavaScript and the Document Object Model Jeremy Keith, Jeffrey Sambells. var count = 1 After the loop has been executed, the value of count will be eleven. It's important The alert message appears ten times. After the  It stops the loop immediately, passing control to the first line after the loop. Namely, alert . The combination “infinite loop + break as needed” is great for situations when a loop’s condition must be checked not in the beginning or end of the loop, but in the middle or even in several places of its body.

var tries;

for (tries = 0; tries < 3; tries++) {
    let User = prompt("Enter your username:");
    let Pass = prompt("Enter your password:");
        if (User === "hello" && Pass === "world") {
            alert("Welcome.");
            break;
        } else {
            alert("Incorrect username and/or password. You have " + tries + " attempt(s) left.");
        }
        if(tries == 2)
        {
            alert("You don't have any attempts left. Your account is now blocked);
        }
}

JavaScript break Statement, When the break statement is used in a loop, it breaks the loop and continues executing the code after the loop (if any). The break statement can also be used  When the break statement is used in a loop, it breaks the loop and continues executing the code after the loop (if any). The break statement can also be used with an optional label reference, to "jump out" of any JavaScript code block (see "More Examples" below).

Perhaps you could achieve this by doing the following:

for (var attemptsRemaining = 3; attemptsRemaining > 0; attemptsRemaining--) {
  
    let User = prompt("Enter your username:");
    let Pass = prompt("Enter your password:");
  
    if (User === "hello" && Pass === "world") {        
      alert("Welcome.");
      break;
    
    } else if(attemptsRemaining <= 1) {          
      alert("To many failed attempts. Your account is now blocked.");
    }
    else {    
      alert("Incorrect username and/or password. You have " + (attemptsRemaining - 1) + " attempt(s) left."); 
    }
  }
}

JavaScript Break and Continue, The break statement can also be used to jump out of a loop. The break statement breaks the loop and continues executing the code after the loop (if any):  The endsWith() method determines whether a string ends with the characters of a specified string. This method returns true if the string ends with the characters, and false if not. Note: The endsWith() method is case sensitive.

How I taught Katy Perry (and others) to program in JavaScript, message will be displayed in a JavaScript Alert box and the program will end: "This code is a good example of the indefinite capabilities of the While Loop," I​  Like the cat, you will also become a Master of JavaScript Looping, after you know all the looping tricks. 1. The “For” Loop. The For Loop is the most basic way to loop in your JavaScript code. It is very handy to execute a block of code a number of times.

JavaScript continue Statement, In this example we use a for loop together with the continue statement. Loop through a block of code, but skip the value of "3": var text = ""; var i; for (i = 0; i < 5;​  There were exactly 3 alert boxes! Let's dissect what happened: var i = 0 is the initial state Before the loop even starts, the variable i is assigned a value of zero 0. i 3 is the conditional statement On every iteration of the loop, we check this comparison. If it's true, we execute the code in the block. If it's false, we exit the loop.

Beginning JavaScript with DOM Scripting and Ajax: From Novice to , message += 'You entered the following:\n'; for ( var i = 0; i < arrayIndex-1; i++ ) total += Number( userNumbers[i] ); } message += 'Total: ' + total + '\n'; alert( message ); Note the loop doesn't end as soon as the user enters 99, but only when the  After that, while loop ends. Here is the syntax for while loop: Syntax: while (condition) { statement(s); } statement(s): A statement that is executed as long as the condition evaluates to true. condition: Here, condition is a Boolean expression which is evaluated before each pass through the loop. If this condition evaluates to true, statement

Comments
  • What happens with this code? It loops forever? You have to let us know what the problem is, what you have tried and where the issue is. Hint: !== is not the same as !=. See stackoverflow.com/questions/1946063/in-javascript-is-same-as
  • Thanks! It's exactly what I needed.