Percebi que mesmo inserindo a condição do número ser maior que 4, podemos ainda ter número ímpar de participantes, o que ocasionaria um problema. Como fazer uma condicional para que a lista seja ser maior que 4 e par?
let amigos= [];
function adicionar() {
let novoAmigo = document.getElementById('nome-amigo');
if (novoAmigo.value == '') {
alert('Informe o nome do amigo!');
return;
}
let listaDeAmigos = document.getElementById('lista-amigos');
amigos.push(novoAmigo.value);
if (listaDeAmigos.textContent == ''){
listaDeAmigos.textContent = novoAmigo.value;
} else {
listaDeAmigos.textContent = novoAmigo.value + ',' + listaDeAmigos.textContent;
}
novoAmigo.value = '';
}
function sortear () {
if (amigos.length < 4) {
alert ('insira mais amigos!');
return;
}
embaralha (amigos);
let listaSorteio = document.getElementById('lista-sorteio');
for (let i =0; i < amigos.length; i++) {
if (i === amigos.length - 1) {
listaSorteio.innerHTML = listaSorteio.innerHTML + amigos[i] + ' --> ' + amigos[0] + '<br>';
} else {
listaSorteio.innerHTML = listaSorteio.innerHTML + amigos[i] + ' --> ' + amigos[i + 1] + '<br>';
}
}
}
function embaralha(lista) {
for (let indice = lista.length; indice; indice--) {
const indiceAleatorio = Math.floor(Math.random() * indice);
// atribuição via destructuring
[lista[indice - 1], lista[indiceAleatorio]] =
[lista[indiceAleatorio], lista[indice - 1]];
}
}
function reiniciar () {
amigos = [];
document.getElementById('lista-amigos').innerHTML = '';
document.getElementById('lista-sorteio').innerHTML = '';
}