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

180
Vistas
How do I solve this with .reduce() only?

Here's a simple task: Find the minimum of an array. Example array: [ -52, 56, 30, 29, -54, 0, -110 ]. I'm trying to solve this with the 'reduce()' higher-order function, WITHOUT using Math.min() or any sorting algorithms/functions. So far I've got this code, x is doing fine and having the right values, but the final iteration returns a weird version of the input array, instead of the minimum:

const input = [-52, 56, 30, 29, -54, 0, -110]

var min = function (list) {
  return list.reduce((x, y) => {
    if (y < x) x = y;
    return x;
  }, 0);
}

console.log(min(input))

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You need to initialize the reduce function with the first element of the array not 0.

var min = function (list) {
    return list.reduce((x, y) => {
        if (y < x) x = y;
        return x;
    }, list[0]);
}

console.log(min([1, 5, 3]))

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