-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (41 loc) · 1.27 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
let listaDeAmigos = [];
let lista = document.getElementById('listaAmigos');
let inputNombre = document.getElementById('amigo');
let resultado = document.getElementById('resultado');
//funcion agregar amigo
function agregarAmigo(){
let nombreDeAmigo = inputNombre.value.trim();
if (nombreDeAmigo === '') {
alert('Debes ingresar un nombre.');
return;
} else {
listaDeAmigos.push(nombreDeAmigo);
inputNombre.value = '';
console.log(listaDeAmigos);
actualizarLista();
}
}
//funcion para actualizar la lista de amigos
function actualizarLista(){
lista.innerHTML = '';
listaDeAmigos.forEach(amigo => {
let item = document.createElement('li');
item.textContent = amigo;
lista.appendChild(item);
})
}
//funcion para sortear amigo secreto
function sortearAmigo(){
if (listaDeAmigos.length === 0){
alert('Debes ingresar al menos un amigo.');
return;
}
let amigoSorteado = listaDeAmigos[Math.floor(Math.random() * listaDeAmigos.length)];
resultado.innerHTML = `<li>El amigo secreto sorteado es : ${amigoSorteado}</li>`
}
//funcion para limpiar la lista de amigos
function limpiarLista(){
listaDeAmigos = [];
actualizarLista();
resultado.innerHTML = '';
}