Numbers that are not repeated in JavaScript fixes

1

I am doing a capture program where a teacher enters your password, if the password has not been registered before then you can register your name but if it is already registered a message will be sent saying "error". However, I'm not very good at the arrangements and flags because I do not understand the concept very well and consequently my program has the error of letting the teacher's name register even though that club is already registered. What am I doing wrong?

var menu_opcion;
var profesores_opcion;
var profesor= [];
var x;
var profesor1;
var renglon=0;
var error=0;
var nombre=[];
do{
menu_opcion=prompt("Ingrese la opcion deseada \n 1)Profesores \n 2)Grupo \n 3)Alumnos \n 4)Reportes \n 5) Calificacion \n 6) Salida")
if(menu_opcion==1)//PROFESORES
{
  profesores_opcion=prompt("MENU PROFESORES \n1)Captura \n2)Consultas \n3)Cambios ")
  if(profesores_opcion==1)
  {
    do{
      error=0;
        renglon=renglon+1;
  profesor1=prompt("Registro Numero :"+  renglon+ "\nIngrese su clave")
     for(x=1; x<=renglon; x++)
               {
                  if(profesor1==profesor[x])
                  {
                     error=1;
                  }
                  if(error===0)
                  {
                    nombre[x]=prompt("Ingrese su primer nombre")
                  }
                  if(error===1)
                  {alert ("Numero ya registrado")
                    
                  }
                  }
               
   
    }while(error!==0)
  }
  
}
}while (menu_opcion!=6)
    
asked by Stephanie B Bautista 19.07.2017 в 04:17
source

1 answer

0

The problem is very likely in

if(profesor1==profesor[x])
{
   error=1;
}

because nowhere do I see that the array profesor is assigned a value, so it does not run and continues to

if(error===0)
{
   nombre[x]=prompt("Ingrese su primer nombre")
}
 Then, there are many things in the code that could be improved:
  • Work with functions, even if required, use classes.
  • In case of several if - else statements, the alternative switch exists.
  • I do not understand very well why the for , if it is to search in an array , it would be better with indexOf or some other function.
  • If in a if you assign the value to one variable and then in another if you compare that value, it would be better to put them together in one, this specifically for:
  • if(profesor1==profesor[x])
    {
       error=1;
    }
    if(error===0)
    {
      nombre[x]=prompt("Ingrese su primer nombre")
    }
    if(error===1)
    {alert ("Numero ya registrado")
    
    }
    

    What would be better:

    if(profesor1==profesor[x])
    {
       error = 1;
       alert ("Numero ya registrado")
    }
    if(error===0)
    {
      nombre[x]=prompt("Ingrese su primer nombre")
    }
    
        
    answered by 19.07.2017 в 04:59