Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="es" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>agenda tablas</title>
    <link rel="stylesheet" href="agenda.css">
  </head>
  <body>
    <div class="entrada">
        <div class="inputs">
          <input type="text" placeholder="cedula" id="ci">
          <input type="text" placeholder="nombre" id="name">
          <input type="number" placeholder="edad" id="age">
          <input type="text" placeholder="sexo" id="sex">
          <input type="number" placeholder="telefono" id="telefono">
          <div class="botonDiv">
            <button type="button" name="button" id="boton">agregar</button>
          </div>
        </div>
        <!-- ordenar por criterio -->
        <div class="botonesOrdenar">
          <button id="botonNombre" onclick="ordenar(1)">Nombre</button>
          <button id="botonCedula" onclick="ordenar(2)">Cédula</button>
          <button id="botonEdad" onclick="ordenar(3)">Edad</button>
        </div>
        <!-- buscar por criterio -->
        <div class="buscar">
          <input type="number" id="buscarCedula" placeholder="buscar por cedula">
          <input type="text" id="buscarNombre" placeholder="buscar por nombre">
          <input type="number" id="buscarEdad" placeholder="buscar por edad">
          <input type="number" id="buscarTelefono" placeholder="buscar por telefono">
        </div>
      </div>
        
     

    <!-- parte por default de la agenda -->
<div class="Principal" id="Principal">
	<div class="defaultPersonaYBorrar">
	<div class="defaultPersonContainer">
		<div class="defaultCaracteristica" id="caracteristica">cedula</div>
		<div class="defaultCaracteristica" id="caracteristica">nombre</div>
		<div class="defaultCaracteristica" id="caracteristica">edad</div>
		<div class="defaultCaracteristica" id="caracteristica">sexo</div>
		<div class="defaultCaracteristica" id="caracteristica">telefono</div>
	</div>
		<a class="enBlanco"></a>
</div>

<!-- en este div se meten los nuevos contactos -->
<div class="principalPersonas" id="principalPersonas">
	
</div>

	
</div>
          <script type="text/javascript" src="agenda.js">

          </script>
  </body>
</html>

              
            
!

CSS

              
                *{
  padding: 0;
  margin: 0;
}


input{
  padding: 5px 10px;
  border-radius: 5px;
  font-size: 15px;
}
button{
  padding: 4px 10px;
  background: linear-gradient(to right,gold,rgb(104, 41, 167));
  border-radius: 5px;
  cursor: pointer;
}

.entrada{
	display: grid;
	grid-template-columns: repeat(3,1fr);
	justify-items: center;
	grid-gap: 15px;
  /* align-items: center; */
  margin-top: 25px;
}
.inputs{
  display: flex;
  flex-direction: column;
}

		.botonesOrdenar{
		
		}

     /*      *=*=*=*=*=*=*      divs      *=*=*=*=*=*=*      */
.Principal{
	/* border: 5px solid aqua; */
}
.personaYBorrar,.defaultPersonaYBorrar{
	/* border: 6px solid rgb(109, 247, 109); */
	display: flex;
	justify-content: space-between;
	align-items: center;
	
}
	.personaYBorrar a{
		width: 7.5%;
		text-align: center;
	}
		.defaultPersonContainer{
	border: 4px solid rgb(65, 65, 65);
	display: flex;
	justify-content: space-between;
	width: 100%;
		}
.personContainer{
	border: 2px solid red;
	display: flex;
	justify-content: space-between;
	width: 100%;
}
	.personContainer.oscurecido{
		background-color: lightgrey;
	}

		.defaultCaracteristica{
			border: 2px solid black;
	width: 100%;
	text-align: center;
	word-break: break-all;
	padding: 8px;
		}
.caracteristica{
	border: 1px solid black;
	width: 100%;
	text-align: center;
	word-break: break-all;
	padding: 5px;
}

	.enBlanco{
		width: 7.5%;
	}

	.contadorCont{
		display: none;
	}
              
            
!

JS

              
                const boton = document.getElementById("boton");
boton.addEventListener('click',agregar)

const principalContainer = document.getElementById('principalPersonas');
//clase para crear datos de personas
class Persona{
  constructor(cedula,nombre,edad,sexo,telefono){
    this.cedula = cedula;
    this.nombre = nombre;
    this.edad = edad;
    this.sexo = sexo;
    this.telefono = telefono;
  }
}

//array donde iran instancias de personas
let personas = [];
function agregar() {
  let cid = document.getElementById('ci')
  let ci = cid
  let name = document.getElementById('name')
  let age = document.getElementById('age')
  let sex = document.getElementById('sex')
  let phone = document.getElementById('telefono');

  personas.push(new Persona(ci.value, name.value, parseInt(age.value) ,sex.value ,phone.value));
  ci.value = ""; name.value = ''; age.value = ''; sex.value = ''; phone.value = '';  
  
dibujar(personas)

}
// variable de contador para  alternar colores
let cont = 0;
function dibujar(arr,encontrado,message){
  if(encontrado == false){
    principalContainer.innerHTML = `no se ha podido encontrar a contacto ${message}`;
  }else{
    let dibuPersona = arr.map( p => `
    <article class="personaYBorrar">
    <div class="personContainer ${cont % 2 == 0 ? 'oscurecido': ''}" id="personContainer">
      <div class="caracteristica">${p.cedula}</div>
      <div class="caracteristica">${p.nombre}</div>
      <div class="caracteristica">${p.edad}</div>
      <div class="caracteristica">${p.sexo}</div>
      <div class="caracteristica">${p.telefono}</div>
    </div>
      <a href="#">delete</a>
  </article>
  <span class="contadorCont">${cont++}</span>  
  `,cont = 0)  
  console.log(cont);
    
    principalContainer.innerHTML = dibuPersona.reduce((a,b)=> a+b,'');
      }

   }

   //buscar indice en contenedor HTML para luego remover
   function getIndex(evento){
      let item = evento.target.parentElement;
      let items = [...principalContainer.querySelectorAll('article')];
      
      return items.indexOf(item);
      }
   principalContainer.addEventListener('click',remover);


   function remover(e) {
     if(e.target.tagName === 'A'){
        personas.splice(getIndex(e),1);
        dibujar(personas);
      
     }
   }
   
  //  funcion para edad/nombre/cedula de los contactos en un array
  // =====================***********====================***************
  // =========***********====================***************============
  //      a partir de aqui es donde quiero refactorizar
  let EDAD = () => personas.map(p => p.edad);
  let NOMBRE = () => personas.map(p => p.nombre);
  let CEDULA = () => personas.map(p => p.cedula);

   function ordenar(opcion) {
     let ordenado;
     if(opcion == 1){
      ordenado = NOMBRE()
      ordenado.sort();
      compareAndPaintNombres(ordenado);
     } 

      else if(opcion == 2){
       ordenado = CEDULA();
       ordenado.sort((a,b)=> a - b);
       compareAndPaintCedulas(ordenado);
     }

     else{
        ordenado= EDAD();
        ordenado.sort((a,b)=> a - b);
        compareAndPaintEdades(ordenado);
     }
     
   }

  //  funcion para comparar los arrays ya ordenados y dibujarlos
  function compareAndPaintNombres(orde){
    let arrComparadoYOrdenado = [];

    
    for(let i = 0; i < personas.length;i++){
      for(let j = 0; j<personas.length;j++){
        if(orde[i] == personas[j].nombre){
     // solo push a personas[j] para que mande todo el objeto/instancia
          arrComparadoYOrdenado.push(personas[j]);
        }
      }
    }
    dibujar(arrComparadoYOrdenado);
  }

  function compareAndPaintCedulas(orde){
    let arrComparadoYOrdenado = [];
    
    for(let i = 0; i < personas.length;i++){
      for(let j = 0; j<personas.length;j++){
        if(orde[i] == personas[j].cedula){
     // solo push a personas[j] para que mande todo el objeto/instancia
          arrComparadoYOrdenado.push(personas[j]);
        }
      }
    }
    dibujar(arrComparadoYOrdenado);
  }

  function compareAndPaintEdades(orde){
    let arrComparadoYOrdenado = [];
    
    for(let i = 0; i < personas.length;i++){
      for(let j = 0; j<personas.length;j++){
        if(orde[i] == personas[j].edad){
     // solo push a personas[j] para que mande todo el objeto/instancia
          arrComparadoYOrdenado.push(personas[j]);
        }
      }
    }
    dibujar(arrComparadoYOrdenado);
  }
  //        aqui  termina donde quiero refactorizar
  // =====================***********====================***************
  // =========***********====================***************============
//=========================================================//
//buscar en la agenda
let buscarCedula = document.getElementById('buscarCedula'),
    buscarNombre = document.getElementById('buscarNombre'),
    buscarTelefono = document.getElementById('buscarTelefono');


buscarCedula.addEventListener('keyup',buscar);

function buscar(evento) {
  if (evento.key === "Enter") {
    let buscado = [],encontrado = true; 
    personas.filter((p) => {
      if(this.value == p.cedula){
        buscado.push(p)
      }
    })
    if (buscado.length == 0) {encontrado = false}
    dibujar(buscado,encontrado,this.value);
  }
}

              
            
!
999px

Console