limit the number of responses entered by the user with prompt

0

I want my prompt se ejecute tres veces until the user responds correctly

response = prompt("capital del italia");
        switch (response) { 
           case 'napoli':
                alert('falso') 
               break;
            case 'lombardia':
                alert('falso')
                break;
            case 'roma':
                 alert("correcto!")
                 break;
            case 'puglia': 
           alert('falso');  
          break;
            case 'sicilia': 
           alert('falso');  
          break;
         
       }
    
asked by steven 01.05.2017 в 19:42
source

1 answer

0

A possible solution to display Prompt three times to give the opportunity to solve the question correctly.

Another detail is that if it is not equal to 'Roma' , you can go directly to default of Switch , in addition to having a variable booleana (correct) that validates us the question was answered correctly and a entero (count) for the number of times the prompt is displayed.

var count=1, correcto =false;
/* Si no se ah respondido
correctamente entra al while*/
while(!correcto){
	/* Si se agotaron las 3 oportunidades mostramos el mensaje
	y termine el Script*/
if(count>3) {
		alert('Se agotaron sus tres Oportunidades'); 
		break;
}
/* Caso Contrario mostramos el Prompt*/
else{
 response = prompt("Capital del italia");
 if(response=='roma'){
  correcto=true;
  alert('correcto');
 }
 else{
  count++;
  alert('incorrecto');
  }
 } 
}
    
answered by 01.05.2017 / 20:02
source