When declaring a variable:

var userChoice = prompt("Выберите цвет: красный, зеленый или синий"); 

you need to make sure that when a person chooses a color, say “red”, a certain action was performed, and if he writes, let’s say “black”, then the color selection window pops up until the person chooses a color from the suggested ones, or click Cancel. .

How many tried can not do it. Reached such a form, now I can not understand how to compare the variable userChoice with the values ​​of the array.

 var userChoice = prompt("Выберите цвет: красный, зеленый или синий"); var choiсes = ["красный", "зеленый", "синий"]; while (userChoice != "красный"){ var userChoice = prompt("Выберите цвет: красный, зеленый или синий"); }; 
  • one
    Add to the question the code of your attempts (albeit not working). - Dmitriy Simushev

2 answers 2

Checks for the existence in the array of an available choice with the entered value, if it is found, it exits the loop

 var userChoice var question = 'Выберите цвет: красный, зеленый или синий'; var choises = ["красный", "зеленый", "синий"]; while (choises.indexOf(prompt(question)) === -1){ userChoice = prompt("Выберите цвет: красный, зеленый или синий"); }; 

An even shorter entry, but it will not respond to the cancel button, and will still require a value:

 var userChoice var question = 'Выберите цвет: красный, зеленый или синий'; var choises = ["красный", "зеленый", "синий"]; while (choises.indexOf(userChoice = prompt(question)) === -1); 

With the cancel button processing

 var userChoice var question = 'Выберите цвет: красный, зеленый или синий'; var choises = ["красный", "зеленый", "синий"]; while ((userChoice = prompt(question)) && choises.indexOf(userChoice) === -1); 
  • one
    It’s better to do { ... } while . One prompt will be less. - Embedder
  • one
    @Embedder I can not disagree, I shortened it even more) - Vasily Barbashev
  • one
    @ Vasily Barbashev, by the way, you didn’t take into account the case " or don’t click Cancel. " - Dmitriy Simushev
  • Gorgeous, thanks) - pavlika
  • one
    @ Vasily Barbashev, you can still apply toLowerCase() to the heap. - Embedder

sort of

 var correctColor = false; while (!correctColor) { var choice = prompt("Выберите цвет: красный, зеленый или синий") switch(choice){ case 'красный': //действия correctColor = true; break; case 'зеленый': //действия correctColor = true; break; case 'синий': //действия correctColor = true; break; default: correctColor = false; } } 
  • actually leave a comment or something minus) it is advisable to argue) - I. Smirnov
  • 2
    Easily. Imagine that you have 1000 options. Will you still write a case for each of them? - Dmitriy Simushev
  • @DmitriySimushev, rightly so. I agree. However, the question featured only 3 options. From this and proceeded, but as a matter of fact, that's right, the solution is far from being the best. Basil's solution is better. - I. Smirnov
  • Here is the minimization of the decision (albeit a correct one) of the solution. This is like a hint to readers that it is better not to do so. - Dmitriy Simushev
  • one
    I use spaces =) Moreover, most of the style guides, from those that I saw, recommend spaces rather than tabs. - Dmitriy Simushev