Created
March 2, 2017 15:19
-
-
Save ivandevp/bcae1ce399d77d349755009cb3c73591 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var numeroEstudiantes = parseInt(prompt("¿Cuántos estudiantes vas a registrar?")); | |
var estudiantes = []; | |
document.write("<h1>Lista de Estudiantes</h1>"); | |
for (var i = 0; i < numeroEstudiantes; i++) { | |
var nombre = prompt("Nombre del estudiante #" + (i+1)); | |
var apellido = prompt("Apellido del estudiante #" + (i+1)); | |
var estudiante = { | |
nombre: nombre, | |
apellido: apellido | |
}; | |
estudiantes.push(estudiante); | |
document.write(nombre + " " + apellido + "<br>"); | |
} | |
var opcion = parseInt(prompt("Ordernar por:\n" + | |
"1. Nombre\n" + | |
"2. Apellido")); | |
var estudiantesOrdenadas; | |
switch (opcion) { | |
case 1: | |
// Ordenar por nombre | |
estudiantesOrdenadas = estudiantes.sort(function (a, b) { | |
return a.nombre > b.nombre; | |
}); | |
break; | |
case 2: | |
// Ordernar por apellido | |
estudiantesOrdenadas = estudiantes.sort(function (a, b) { | |
return a.apellido > b.apellido; | |
}); | |
break; | |
default: | |
// Opción inválida | |
alert("Opción inválida"); | |
break; | |
} | |
document.write("<h1>Lista de Estudiantes Ordernada:</h1>"); | |
for (var i = 0; i < numeroEstudiantes; i++) { | |
document.write(estudiantesOrdenadas[i].nombre + " " + estudiantes[i].apellido + "<br>"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment