Ejercicio muy sencillo extraido del curso:
Explicación del ejercición: for this exercise you will be making a very simple web app that contains an array of student names in a .js file. The website will prompt you for four possible commands:
- add: will then create a prompt for a student name request. Then add this name to the student to the array of student names in the .js file.
- remove: will create a prompt for a student name request. Then remove this name from the roster array.
- display: will print out the roster using console.log
- quit: will end the while loop of prompts.
Código Javascript:
let option = "";
let students = [];
while (option != 'quit') {
option = prompt("Seleccione una opcion: ");
switch (option) {
case 'add':
student = prompt("Introduzca el nombre:" );
addStudent(student);
break;
case 'remove':
student = prompt("Introduzca el nombre:" );
removeStudent(student)
break;
case 'display':
displayStudents();
break;
case 'quit':
break;
default:
alert ("Opcion desconocida")
break;
}
}
function addStudent (nombre) {
students.push(nombre);
}
function removeStudent (nombre) {
let newStudents = [];
for(let i=0;i<students.length;i++)
if (students[i]!=nombre)
newStudents.push(students[i]);
students = newStudents;
}
function displayStudents () {
console.log(students)
}