Arrangements in JavaScript

1

How can I do that in my program the same name can not be added in the name array [x]. so that it is in the same way as the ID, that is, if the same name is registered more than once I get the message "NAME ALREADY REGISTERED" and do not allow me to continue the record unless I change the name.

var opcion_menu;
var aux_idprof;
var id_prof=[];
var nombre=[];
var aux_nombre;
var x=0;
var y=0;
var z=0;
var renglon=0;
var activo=0;
var menu_profesores;
var ya_esta=0;

var ya_estanombre=0;



do{
  opcion_menu=prompt ("Ingrese la opcion deseada: \n 1.Profesores \n 2.Grupo/Materia \n 3.Alumnos \n 4.Reportes \n 5.Calificaciones \n 6.Salida");
  
  if(opcion_menu==1){
  
    do{
   menu_profesores=prompt("Ingrese la opcion deseada: \n1.Captura \n2.Consultas \n3.Cambios \n4.Cancelaciones \n5.Regreso");
    if(menu_profesores==1)//CAPTURAS PROFESORES
    {
     renglon=renglon+1; 
    do{
      ya_esta=0; 
   aux_idprof=prompt("Ingrese su ID"+"\n"+"Registro no.: "+renglon)
    for(x=0;x<=renglon;x++){
      if(aux_idprof==id_prof[x]){
        ya_esta=1;
        alert("ID ya Registrado")
      }
    }
  }while(ya_esta!==0);
  if(ya_esta!=1){ 
    id_prof[renglon]=aux_idprof; 
    
   do{
      ya_estanombre=0; 
   aux_nombre=prompt("Ingrese su nombre completo")
    for(x=0;x<=renglon;x++){
      if(aux_nombre==nombre[x]){
        ya_estanombre=1;
        alert("Nombre ya Registrado")
      }
    }
   
  }while(ya_estanombre!==0);

  
  }
 
  
  }//Registrar profesor
      
    }while(menu_profesores!=5);
  }//MENU PROFESORES
 
  if(opcion_menu==2){}//MENU MATERIA
  if(opcion_menu==3){}//MENU ALUMNOS
  if(opcion_menu==4){}//MENU REPORTES
  if(opcion_menu==5){}//MENU CALIFICACIONES
  
  
  
  }while(opcion_menu!=6);
    
asked by Stephanie B Bautista 28.07.2017 в 16:12
source

1 answer

0

Use the indexOf method that returns the index of the element if it exists, otherwise it will return -1:

var nombres = [];
while(true)
{
 var nombre = prompt("Ingrse el nombre \n escriba 0 para salir");
 
  if(nombre == "0")
  {
     break;
  }
  if(nombres.indexOf(nombre) == -1 && nombre != null)
  {
    nombres.push(nombre);
  }
  else
  {
    alert("El nombre ya fue registrado.");
  }
}

console.log("Nombres registrados");
for(var i = 0; i < nombres.length;i++)
{
  console.log(nombres[i]);
}

You'll just have to adapt it to your code and that's it.

    
answered by 28.07.2017 в 16:27