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

113
Visualizações
indexOf() skipped repeated element javascript

I am trying to print the values of odd positions in the array below but it skipped 7, what could be wrong, please?

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

const validateCred = (serialDigits)=>{
  //let sum = 0;
  for (let i = serialDigits.length - 1; i >= 0; i--){
    let currentDigit = serialDigits[i];
    
    if( serialDigits.indexOf(currentDigit) % 2 === 1){
      console.log(currentDigit)
        };
    }
}

console.log(validateCred(valid1));
// 0 6 0 0 6 3 4
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This:

if( serialDigits.indexOf(currentDigit) % 2 === 1){

Is not a good line because indexOf find the first occurrence in the array, therefore, do not fulfil your need. just use the variable i instead:

if (i % 2 === 1) {

Similarly, if you want to print the even indexes, you can use this code:

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

const validateCred = (serialDigits) => {
  //let sum = 0;
  for (let i = serialDigits.length - 1; i >= 0; i--) {
    let currentDigit = serialDigits[i];

    if (i % 2 === 0) {
      console.log(currentDigit);
    };
  }
}

validateCred(valid1);
// 0 6 0 0 7 6 3 4

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