Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda