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

176
Views
When using using reduce with ternary inside a map, I seem to have troubles

this is to return an array of numbers that are the highest value of the arrays inside of the base array. I can get it to work when i use for statements. But I have tried to simplify it, and can't work out why it doesn't work. any help would be appriciated.

    function largestOfFour(arr) {
      return arr.map((x) => x.reduce((a, c) =>  c > a ? c : a, 0));
    }

Example with input and output:

const input = [
  [1,2,3,4,5],
  [6,5,4,3,2,1],
  [1,7,3,4,5,6],
];

function findHighestElements(arr) {
    return arr.map((x) => x.reduce((a, c) =>  c > a ? c : a, 0));
}

console.log(findHighestElements(input)); // [5,6,7]

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You don't need to reduce, you can do it by just Math.max. like this:

function findMaxNumbers(arr) {
  return arr.map((x) => Math.max(...x));
}

let test = [[1, 2, 3],[4, 5, 6],[7, 8, 9]];
console.log(findMaxNumbers(test));

about 4 years ago · Juan Pablo Isaza Report

0

If you have values smaller than zero, you need to remove the start value

x.reduce((a, c) =>  c > a ? c : a, 0)
                                   ^

or use a very small start value, like -Number.MAX_VALUE.

about 4 years ago · Juan Pablo Isaza Report

0

To get the max of all the maxes, you can reduce the reductions. If you just want the max's, map the reduction.

const maxOfArray = a => a.reduce((a, c) => c > a ? c : a, -Number.MAX_SAFE_INTEGER); // thanks to Nina
const conciseVLAZMax = a => Math.max(...a); // thanks to VLAZ 

let a = [
  [1, 2, 3],
  [6, -1, -2],
  [3, 4, 5],
]

let maxs = a.map(maxOfArray);
let maxMax = maxOfArray(maxs);

console.log(maxs);
console.log(maxMax);

about 4 years ago · Juan Pablo Isaza 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!