-2

I'm trying to make it so that I can exit this program with exit even if someone enters EXIT ExIt, EXit, etc. I tried putting it at the end of the var input at the beginning of the string and at the end of the string in the "}while" statement and it still not getting it to work. Does anyone know where I should put it?

<!DOCTYPE html>
<html>
<head>
<title>Project 1 – Michael Fiorello</title>
</head>
<body>
<h1>Robot Speak</h1>
<script>
    do{
    var input = prompt ("Please enter 1, 2, 3, or exit.");{
    if (input == "1")
         {
             alert ("THANKS!")
         }
    else if (input == "2")
        {
        alert ("COOL!")
        }
    else if (input == "3")
        {
        alert ("AWESOME!")  
        }
    else if (input == "exit")
        {
       alert ("Okay") 
        }
    else 
        {
        alert ("You need to enter something")
        console.warn("You need to enter something");
        }
        }
        }while(input != "exit")
</script>
</body>
</html>
user229044
  • 232,980
  • 40
  • 330
  • 338

2 Answers2

1
else if (input.toLowerCase() == "exit")
{
   alert ("Okay") 
}

As pointed out in the comments, if the user cancels the prompt, the result will be null. To catch this case, you should test for this value as well before coming to the line i wrote above:

if (input == null){
    handleCancel();
}
...
else if (input.toLowerCase() == "exit")
{
   alert ("Okay") 
}

In case you want "exit" and a press on cancel treated as program termination, you start testing for that case:

var goOn = true;

while(goOn){

    var input = prompt ("Please enter 1, 2, 3, or exit.");
    if (input == null || input == "exit")
    {
         goOn = false;
         // handle cancel
    }
    else if (input == "1")
         {
             alert ("THANKS!")
         }
    else if (input == "2")
         {
            alert ("COOL!")
         }
    else if (input == "3")
         {
             alert ("AWESOME!")  
         }
}
Psi
  • 6,387
  • 3
  • 16
  • 26
0

toLowerCase() may be invoked after null checking (i.e. when the prompt is cancelled) and when any case-insensitive comparisons are taking place.

To reduce the amount of comparisons, you may also use an infinite loop with a break upon hitting the required response.

You've also added an extra unneeded block, starting after prompt. This may be removed.

while (true) {
    var input = prompt("Please enter 1, 2, 3, or exit.");
    if (input == null){
        // Handle failure to specify a value
    } else if (input == "1") {
        alert("THANKS!");
    } else if (input == "2") {
        alert("COOL!");
    } else if (input == "3") {
        alert("AWESOME!");
    } else if (input.toLowerCase() == "exit") {
        alert("Okay");
        break;
    } else {
        alert("something else");
        console.warn("You need to enter something");
    }
}
elixenide
  • 44,308
  • 16
  • 74
  • 100
Ren
  • 106
  • 1
  • 3
  • This will have an error as well, if the user cancels the prompt. Additionally, it will always call toUpperCase which is an overhead when there are mostly numbers expected. No need to call it every time... – Psi Feb 27 '17 at 00:56
  • Watch the profanity. Read the ["Be Nice"](http://stackoverflow.com/help/be-nice) policy. – elixenide Aug 07 '17 at 02:31