Home > Blockchain >  JavaScript looping issue in the middle of looping
JavaScript looping issue in the middle of looping

Time:11-19

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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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.

CodePudding user response:

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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]))
}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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

CodePudding user response:

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);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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);
<iframe name="sif6" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related