Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I loop back to the prompt if answer is incorrect?

Tags:

javascript

How do I loop back to the prompt question if the user enters the wrong answer and I want the question to repeat until they get the correct answer?

<html>

<head>
    <script>
    </script>
    <title> Javascript program</title>
</head>

<body>
    <script>
        var company = (prompt("What the name of the company that developed the javascript language?", ""));
        if (company == 'netscape') {
            alert("correct answer!");

        } else {
            alert("wrong answer");

        }
    </script>
</body>

</html>
like image 840
jmike Avatar asked Dec 29 '25 07:12

jmike


1 Answers

You can wrap your code in a function and call self on incorrect value.

var max_count = 5;

function showConfirm() {
  var company = (prompt("What the name of the company that developed the javascript language?", ""));
  if (company == 'netscape') {
    alert("correct answer!");
  } else {
    alert("wrong answer");
    // to limit user for limited count
    if (--max_count > 0)
      showConfirm()
  }
}
showConfirm();
like image 92
Rajesh Avatar answered Dec 30 '25 20:12

Rajesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!