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

82
Visualizações
IF Statment Logic Meaning

I am working on a project but I was stumped to see this piece of code if ((array.length - 1 - i) % 2 === 1) I am just curious as to how it works?

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];

const validateCred = (array) => {
  let total = 0;
  for (var i = array.length - 1; i >= 0; i--) {
    // console.log(array[i]) 
    if ((array.length - 1 - i) % 2 === 1) { /*I would like to know what does this do when put in a if statment array.length - 1 - i  */
      array[i] *= 2;
      // console.log(array[i]) 
      if (array[i] > 9) {
        array[i] -= 9;
      }
    }
    total += array[i]
    console.log(total)
  }
  return total % 10 === 0;

};
console.log(validateCred(valid1))

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

0

  • array.length - 1: the last index of the array
  • lastIndex - i: work through the array from back to front (typically)
  • someIndex % 2 === 1: is the index modulus 2 equal to 1 (meaning, is it odd?)

But then the loop iterates from the last index to the first index, so the - i just makes it iterate from front to back again. Which seems needlessly complex.

This code is pretty funky...

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