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

241
Vistas
¿Cómo puedo resolver este problema con array.prototype?

Estoy en un pequeño problema aquí. Realmente no puedo resolver este problema, he estado usando esta solución:

 Array.prototype.all = function(callback){ for(let i = 0; i < this.length; i++){ if(this[i] < 10) return true; } if(this[i] < 2) return false; } }

Pero no funciona. Sé que estoy cometiendo un error de novato (soy nuevo en aprender javascript), y me encantaría un poco de ayuda con esto.

El problema en cuestión es este:

 function all(fn) { // Write a function called all in the Array prototype, // that receives a function (callback). Assume fn always returns true or false. // The function all must return true if fn returns true when we invoke it passing all the elements of the array one by one // the function all must return false, if any element of the array causes fn to return false; // Eg: // [1,2,3].all(function(elem) { // return elem < 10; // }); // returns true // [1,2,3].all(function(elem) { // return elem < 2; // }); // returns false. }
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Solo tiene que devolver true si el valor devuelto de la callback de llamada es false ; de lo contrario, return true

 Array.prototype.all = function (callback) { for (let i = 0; i < this.length; ++i) { const result = callback(this[i]); if (!result) return false; } return true; }; const arr1 = [1, 2, 3, 4, 20, 300, 0]; const arr2 = [1, 0]; console.log(arr1.all((elem) => elem < 2)); console.log(arr2.all((elem) => elem < 2));

ADDITIONAL INFO

Array.prototype ya tiene every métodos que harán exactamente lo mismo

 const arr1 = [1, 2, 3, 4, 20, 300, 0]; const arr2 = [1, 0]; console.log(arr1.every((elem) => elem < 2)); console.log(arr2.every((elem) => elem < 2));

about 4 years ago · Santiago Trujillo 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