Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

84
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!