Problem when finishing program when typing word in javascript

0

When I ask the question "Enter another name to check if it is found or not" when typing end, you must end the program, but it does not. What should I modify?

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8" />
<title>Ej 2</title>
</head> 
<body>

<script type="text/javascript">

var nombres=[];
var respuesta;


for (var i=1; i<=10; i++) { 
    nombres[i]=prompt("Introduce el nombre " +i);
}

var comprueba=prompt("Introduce otro nombre más para comprobar si se encuentra o no");

do {
var posicion=0;
for (var i=1; i<=10; i++) {
    if(comprueba==nombres[i]) {
        posicion=i;
     }
   }  

if(posicion!=0) {
alert("El nombre " +comprueba+ " ha sido encontrado en la posición " +posicion);
}else{
alert("El nombre " +comprueba+ " no ha sido encontrado");
}   


}while(respuesta=="fin")

</script>
</body>
</html>
    
asked by user09b 17.10.2018 в 13:02
source

2 answers

1

I think there are several things to consider. As you have been told in the comments:

  • The do-while is not well done. You are running the code contained in the while respuesta == "fin" when you have to do the opposite, when the user enters "end" is when the do-while ends.

  • You have not defined the value of respuesta because you are not asking at any time so it will always be undefined .

  • If you want to repeat the loop do-while n times, you will also have to ask for the new word to search within that loop

  • Extra comments :

  • Get used to using let more often. var creates global variables which can give you problems of conflict between variables or unwanted behavior. More info here and here .

  • Get used to using the strict comparators (=== and! ==) that not only compare the value but also the type of object. More info here and here .

  •  <!DOCTYPE html>
        <html lang="es">
        <head>
        <meta charset="utf-8" />
        <title>Ej 2</title>
        </head> 
        <body>
    
        <script type="text/javascript">
    
        let nombres=[];    
    	let respuesta;
    
        for (let i=1; i<=10; i++) { 
            nombres[i]=prompt("Introduce el nombre " +i);
        }
    
        do {
        let comprueba=prompt("Introduce otro nombre más para comprobar si se encuentra o no");
        
        let posicion=0;
        for (let i=1; i<=10; i++) {
            if(comprueba==nombres[i]) {
                posicion=i;
             }
           }  
    
        if(posicion!==0) {
        	alert("El nombre " +comprueba+ " ha sido encontrado en la posición " +posicion);
        }else{
        	alert("El nombre " +comprueba+ " no ha sido encontrado");
        }   
    
    	respuesta = prompt("Si desea finalizar introduzca 'fin'");
    
        }while(respuesta.toLowerCase()!=="fin")
    
        </script>
        </body>
        </html>

    You could do more things like compare the string entered is a text other than empty, for example, but I leave that to you;)

        
    answered by 17.10.2018 в 13:29
    0

    There are three problems in your code:

    1.- Just ask the user once a name to check if it exists in the list. Therefore, once it enters the do-while, an infinite loop occurs, since it NEVER changes that name.

    2.- you have the variable comprueba to read what the user asks, but then the loop checks with respuesta that has NEVER changed in your code.

    3.- your check in the loop is ==. This means that it will only be in the loop if what your user types is fin . In case of writing a name, you would not fulfill the condition and it would get out of the loop. The correct condition would be != . To solve 1, you can do:

    do {
    var comprueba=prompt("Introduce otro nombre más para comprobar si se encuentra o no");
    

    That is, first the do, then the var checks.

    to solve 2, instead of using respuesta in the condition of the loop, change it by comprueba :

    }while(respuesta!="fin")
    

    to solve 3, you must understand that do-while is hacer-mientras , that is, as long as respuesta IS NOT end, you want to repeat the loop. Therefore, the condition is with! =.

        
    answered by 17.10.2018 в 13:25