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

86
Visualizações
JavaScript looping issue in the middle of looping

Hey guys I m trying to loop over the array of numbers, and what i want my function to do is whenever it is position of a number in array is EVEN number I want a program to return me 'Yan' word in stead of number. but when the position of a number is ODD number i want it to return just a number.

But for some reason in the middle of the array I am getting 'Yan' word instead of getting a number.

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
const reversValid1 = valid1.reverse()

const myArray = []

const validateCard = (arr) =>{
    for(let i = 0; i < arr.length; i++){
        if(arr.indexOf(reversValid1[i])%2 == 1){
            console.log('Yan')
        }
        else{
            console.log(arr[i])
        }
    }
}


validateCard(reversValid1)

I m getting the following response:

8 Yan 8 Yan 1 Yan 8 Yan 9 Yan Yan Yan 9 Yan 5 Yan

You can see i m getting 3 Yan consecutively.

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

0

Why instead of use indexOf don't use directly i ?

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
const reversValid1 = valid1.reverse()

const myArray = []

const validateCard = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    if (i % 2 == 1) {
      console.log('Yan')
    } else {
      console.log(arr[i])
    }
  }
}


validateCard(reversValid1)

The problem is indexOf will find multiple index of same number.

Example:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8]
const reversValid1 = valid1.reverse()
for (let i = 0; i < reversValid1.length; i++) {
  console.log(reversValid1[i], reversValid1.indexOf(reversValid1[i]))
}

As you can see the same number have same index instead of index of array.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.forEach:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const reversValid1 = valid1.slice().reverse();

const validateCard = (arr) => {
  arr.forEach((v,i)=>{
    if (i%2 == 0) {
      console.log('Yan');
    } else {
      console.log(v);
    }
  });
};

validateCard(reversValid1);

about 4 years ago · Juan Pablo Isaza Relatório

0

This might be what you want:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const reversValid1 = valid1.reverse();

const myArray = []

const validateCard = (arr) => {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] % 2 === 0) {
            console.log('Yan');
        }
        else {
            console.log(arr[i]);
        }
    }
}


validateCard(reversValid1);

Or this:

const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const reversValid1 = valid1.reverse();

const validateCard2 = (arr) => {
    for (let i = 0; i < arr.length; i++) {
        if (i % 2 === 0) {
            console.log('Yan');
        }
        else {
            console.log(arr[i]);
        }
    }
}

validateCard2(reversValid1);

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