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

132
Visualizações
How find the array index for two array comparision and return once array item less than another?

I would like to compare two array then return the index once first array less than second array. But if the start value of first array greater than the second array, must skip until first array value less than second array. For example

case1: This must return index = 2 (exampleArr1 < exampleArr2 at index =2)

   var exampleArr1 = [15,9,7,5,3,1];
   var exampleArr2 = [2,6,8,12,17,22];

  function compareArray(exampleArr1,exampleArr2){
    ...
    return result
  }

case2: This must return index = 6 (exampleArr1 < exampleArr2 at index =6)

   var exampleArr1 = [1,2,4,5,15,9,7,5,3];
   var exampleArr2 = [2,3,5,6,2,6,8,12,17];

  function compareArray(exampleArr1,exampleArr2){
    ...
    return result
  }

Any advice or guidance on this would be greatly appreciated, Thanks.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

A simple for loop will suffice. In our loop, we already have the index so we just compare the first array at this index with the second array at this index.

const compareArray = (arr1, arr2) => {  
  let startIndex = 0
  if (arr1[0] < arr2[0]) {
    // find the index from arr1 that is greater than arr2
    startIndex = arr1.findIndex((a, index) => a > arr2[index])
  }

  for (let i = startIndex; i < arr1.length; i++) {
    if (arr1[i] < arr2[i]) {
      return i
    }
  }
}

const exampleArr1 = [15,9,7,5,3,1]
const exampleArr2 = [2,6,8,12,17,22]
const exampleArr3 = [1,2,4,5,15,9,7,5,3]
const exampleArr4 = [2,3,5,6,2,6,8,12,17]

console.log(compareArray(exampleArr1, exampleArr2))
console.log(compareArray(exampleArr3, exampleArr4))

about 4 years ago · Juan Pablo Isaza Relatório

0

Please use the findIndex method in your function like

var exampleArr1 = [15,9,7,5,3,1];
var exampleArr2 = [2,6,8,12,17,22];

function compareArray(exampleArr1,exampleArr2) {
    if (exampleArr1[0] < exampleArr2[0]) {
        exampleArr1 = exampleArr1.slice(1);
    }
    return exampleArr1.findIndex(function(e, i) { return exampleArr2[i] > e; });
}

console.log(compareArray(exampleArr1, exampleArr2));

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