Infinite loop in Javascript

0

For some reason in my program an infinite loop is being generated and this does not allow me to run it.

var menu_opcion;
var profesores_opcion;
var profesor = [];
var x;
var profesor1;
var renglon = 0;
var error = 0;
var yaesta = 0;
var nombre = [];
var primer_apellido = [];
var activo = 0;
var renglon = 0;
var auxnombre;
var auxa_primerapellido;

do {
    yaesta = 0;

    menu_opcion = prompt("1. Proferores \n2.Grupo \n3.Alumnos \n4.Reportes \n5.Calificacion \n6.Salida ");
    do {
        if (menu_opcion == 1) {
            profesores_opcion = prompt("1. Captura \n2.Consultar \n3.Cambios \n4.Cancelaciones\n5.Salida")
            if (profesores_opcion == 1) {
                renglon = renglon + 1;
                do {
                    error = 0; //la variable error se iguala a 0 para evitar un bucle infinito.
                    profesor1 = prompt("Ingrese su clave" + "\n" + "Registro no.: " + renglon)
                    for (x = 1; x <= renglon; x++) {
                        if (profesor1 == profesor[x]) {
                            error = 1;
                            alert("Profesor ya registrado")
                        }
                    }
                } while (error !== 0);

                if (error != 1) {
                    profesor[renglon] = profesor1;
                    error = 0;
                    auxnombre = prompt("Ingrese su nombre")
                    nombre[renglon] = auxnombre.toUpperCase();
                    primer_apellido[renglon] = prompt("Ingrese su Primer apellido")
                    primer_apellido[renglon] = aux_primerapellido.toUpperCase();

                    activo[renglon] = 1;
                }
            }
        }
    } while (profesores_opcion != 5)
} while (menu_opcion != 6)
    
asked by Stephanie B Bautista 19.07.2017 в 04:54
source

1 answer

2

You only leave the inner loop if profesores_opcion is 5 , but you only assign a value if menu_opcion is 1 .

In fact, the sequence 1-5-6 comes out without any problem (in the first iteration of the inner loop you assign the value of profesores_opcion to 5 , which still exists in the second iteration of the inner loop).

Solution: You have to make sure that the internal logic does not give you any case where the expression of the loop is true but the values on which that expression depends can not be modified. For example, add a else to if (menu_opcion == 1) that assigns 5 to profesores_opcion 1 .

In any case, the structure is rare. The most normal thing would be to put the loop inside the if (menu_opcion==1) , so that only the loop that depends on the value of opcion_profesor would be entered in the case that opcion_profesor makes sense.

Additionally:

  • With a pair of alert and console.log investigating the values of the variables at each important point you probably would have found the problem yourself; in the worst case the question could have been answered immediately.

  • As @ArieCwHat insinuates, the code begins to be quite unmanageable (and as proof, the fact that you could not read correctly what it does). Start using features or it will get worse every time (and no one here is going to help you debug a 200-line code).

1 We must be careful not to take into account only the values we expect, but all possible ones. That is, it is not enough to cover the "valid" possibilities (1-5) but also all the possible ones (a user can write "20" or "hello", and the program should not hang).     
answered by 19.07.2017 в 11:22