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

163
Visualizações
How can I find all possible combinations of elements in an array

If we start with array [1, 2, 3, 4] and getting the following result:

letresult = [[1],[2],[3],[4],[1,2],[1,3],[1,4],[2,3],[2,4],[3,4],[1,2,3],[2,3,4],[1,2,3,4]];

So that later you can find the most suitable variant of the sum of the numbers in the array according to the specified condition (for example, no more than 8)

I don't understand how to generate all possible combinations from array [1,2,3,4] to get result I wrote above. Or if i have not only 4 numbers in array but [1,2,3,....,9,10] and how to generate all possible 3 digit numbers.

const findNumber = function(z, array) {
  let sum = [];

  for (let i = 0; i < array.length; i++) {
    let reducer = array[i].reduce((a, b) => a + b);
    if (reducer < z) {
      sum.push(reducer)
    }
  }
  let max = Math.max.apply(Math, sum);
  console.log(sum);
  console.log(max);
  console.log('index in array ', sum.indexOf(max))
}
findNumber(8, [
  [1],
  [2],
  [3],
  [4],
  [1, 2],
  [1, 3],
  [1, 4],
  [2, 3],
  [2, 4],
  [3, 4],
  [1, 2, 3],
  [2, 3, 4],
  [1, 2, 3, 4]
]);

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