1
resposta

[Bug] Nenhuma resposta ao clicar no botão 'sortear'.

no seguinte código da aula 5 de lógica de programação. Não tenho nenhuma resposta ao executar a função 'sortear'.

let amigos = [];

function adicionar() {
    let nomeAmigo = document.getElementById('nome-amigo');
    listaAmigos = document.getElementById('lista-amigos');
    if (nomeAmigo.value == '') {
        alert('coloque um nome');
    } else {
        if (nomeAmigo.value == amigos.includes) {
            alert('já esta em uso');
        } else {
    amigos.push(nomeAmigo.value);
    if (listaAmigos.textContent == ''){
        listaAmigos.textContent = nomeAmigo.value;
    } else {
        listaAmigos.textContent = listaAmigos.textContent + ', ' + nomeAmigo.value;
    }
    //listaAmigos.push(listaAmigos);
    nomeAmigo.value = '';
}
}
}

function sortear(amigos) {
    embaralha(amigos);
    let sorteio = document.getElementById('lista-sorteio');

    //código omitido

for (let i = 0; i < amigos.length; i++) {
    if (i == amigos.length - 1) {
        sorteio.innerHTML = sorteio.innerHTML + amigos[i] + ' --> ' + amigos[0] + '<br>';
    } else {
        sorteio.innerHTML = sorteio.innerHTML + amigos[i] + ' --> ' + amigos[i + 1] + '<br>';
        }
}

//código omitido
}

function embaralha(listaAmigos) {

    for (let indice = listaAmigos.length; indice; indice--) {

        const indiceAleatorio = Math.floor(Math.random() * indice);

        // atribuição via destructuring
        [listaAmigos[indice - 1], listaAmigos[indiceAleatorio]] = 
            [listaAmigos[indiceAleatorio], listaAmigos[indice - 1]];
    }
}

function reiniciar() {
    amigos = []
    document.getElementById('lista-amigos').innerHTML = '';
    document.getElementById('nome-amigo').innerHTML = '';
}
1 resposta

Bom dia!

Compartilhe no forum todo o código que você tem. Somente com essas partes não é possível ajudá-lo na solução.

Bons estudos!