Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!