Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
every() method within arrays! New to programming

I'm not understanding why this doesn't work

function every(arr, callback) {
    for (let i = 0; i < arr.length; i++) {
        if (callback(arr[i]) === true) {
            return true
        }else return false
    }
}

can someone help me understand how the every() method works. I need to write the source code itself for the method in relation to the following 2 questions.

describe('every', function () {
        it('returns true when the callback returns true for all elements', function () {
            const original = ['ace', 'cat', 'dog', 'pit']
            const result = every(original, (el) => el.length === 3)
            expect(result).to.be.true
        })

        it("returns false when the callback doesn't return true for all elements", function () {
            const original = ['ace', 'cat', 'parrot', 'dog']
            const result = every(original, (el) => el.length === 3)
            expect(result).to.be.false
        })
    })
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're only testing the first element of the array, because you return either true or false inside the loop.

You should return false immediately if the test fails. Otherwise, you need to keep looping. If you get through the entire loop, then you can return true.

function every(arr, callback) {
    for (let i = 0; i < arr.length; i++) {
        if (!callback(arr[i])) {
            return false;
        }
    }
    return true;
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda