Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda