Suppose a variable is set:

var variable1 = prompt ('Write "2", please'); 

It is necessary that, if the user enters a different value than "2", an error message appears. This can be done with if .

The question is how to make sure that after the error message, each time (cyclically) the prompt again with the same question, and so that the value entered in it is given to the variable1 ?

    1 answer 1

    Enclose the prompt , for example, while

     while ( true ) { var variable1 = prompt ('Write "2", please'); if ( variable1 == 2 ) break; alert( "Error. Invalid value" ); } 
    • four
      @MaximVelichkin But not for that. We, novice programmers, should help each other. :) - Vlad from Moscow
    • That's for sure, thanks)! The only thing is that after the execution of the cycle, the code editor hangs for me for some reason, I checked it twice, strangely, it seems that no endless cycles are created ... In the browser, everything works without interference, apparently the problem is in the editor. - Rumata
    • one
      @MaximVelichkin I can't say anything about this. But keep in mind that the variable will contain the string "2", not a number. - Vlad from Moscow
    • Understood thanks! - Rumata