How are arrays indexed?

I’m trying to iterate backward through an Array using a for loop. What I want to know is how the for loop indexes the array while doing so. Are the elements indexed in a reverse manner as well? Does the last element get an index of 0?

Here is a program I wrote to check it for myself but it doesn’t behave as expected and I’m having a serious headache.

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

const arrLength = testArr.length - 1;

const arrIndex = [];

const indexCheck = (arr) => {
  for (let i = arrLength; i >= 0; i--) {
  	arrIndex.push(arr.indexOf(i));
	}
  return arrIndex;
}

console.log(indexCheck(testArr));

Here is the output for the above program:

[ -1, -1, 8, 7, 10, 5, 4, 3, 2, 1, 0, 11 ]

The for loop doesn’t index the array, the indices are a static aspect of the array object. The parameters of your for loop simply define the bounds over which you’re iterating.

What you’re doing is you’re starting with i = 11 (because the array has 12 objects, and you’ve subtracted one), and decrementing i with each iteration.

You’re then pushing the return value of arr.indexOf(i) to your arrIndex array. .indexOf() checks whether the given element exists within the array, and if so returns the index of the first occurrence of that element. So, your first iteration is doing:

arrIndex.push(arr.indexOf(11))

There is no 11 in your array, so .indexOf() returns -1 and this is added to your arrIndex array. Same for 10, because that doesn’t exist in the array either.

So, we arrive at 9 - which exists at position 8, as shown by your output. Remember, arrays are indexed from 0, so whilst 9 is the 9th item in the list it’s index is 8.

The code segment you’ve posted is behaving correctly, as far as I can tell.

To answer your question:

To the best of my knowledge, no. There are languages, like Python, where you can iterate over a list (a Python “array”) backwards by providing a negative index. JavaScript does not support a similar functionality as far as I am aware, so the indices are always incremented from left to right starting at 0.

If you were just looking to iterate backwards over an array and push to a new one to reverse its order, then you could do this:

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

const invertArray = arr => {
    let reverseArray = [];
    for (let i = arr.length - 1; i >= 0; i--) {
        reverseArray.push(arr[i]);
    }
    console.log(reverseArray);
    return reverseArray;
}

let rrAtset = invertArray(testArr);

This outputs the following: [0, 7, 4, 9, 8, 6, 6, 5, 4, 3, 2, 1].

That any help? :slight_smile:

1 Like

That was truly relieving. But how do I store just the indices of an array to another array if I want to?

And can’t we simply use a reverse method on an array to reverse it? I know it mutates the original array but we can use the splice method to create a new array which we can then use with the reverse method.

Again thank you for helping me stay sane.:sweat_smile:

1 Like

What is it that you’re trying to accomplish? You’ve already got some code which will test for the presence of an item in the array, and if so return its index. Though, keep in mind that .indexOf() only returns the index of the first item (i.e. lowest index value) it finds.

Probably; you didn’t really say what you’re actually trying to do, just how you were confused around array indexing. I had to make a guess as to why you wanted to iterate backwards over the array, so I could provide an example. :slight_smile:

1 Like

I was doing this project wherein I needed to apply the Luhn algorithm in order to check the validity of a credit card. But I was unable to figure out a way to reverse iterate through the credit card array and double every alternate digit.

// All valid credit card numbers
const valid1 = [4, 5, 3, 9, 6, 7, 7, 9, 0, 8, 0, 1, 6, 8, 0, 8];
const valid2 = [5, 5, 3, 5, 7, 6, 6, 7, 6, 8, 7, 5, 1, 4, 3, 9];
const valid3 = [3, 7, 1, 6, 1, 2, 0, 1, 9, 9, 8, 5, 2, 3, 6];
const valid4 = [6, 0, 1, 1, 1, 4, 4, 3, 4, 0, 6, 8, 2, 9, 0, 5];
const valid5 = [4, 5, 3, 9, 4, 0, 4, 9, 6, 7, 8, 6, 9, 6, 6, 6];

// All invalid credit card numbers
const invalid1 = [4, 5, 3, 2, 7, 7, 8, 7, 7, 1, 0, 9, 1, 7, 9, 5];
const invalid2 = [5, 7, 9, 5, 5, 9, 3, 3, 9, 2, 1, 3, 4, 6, 4, 3];
const invalid3 = [3, 7, 5, 7, 9, 6, 0, 8, 4, 4, 5, 9, 9, 1, 4];
const invalid4 = [6, 0, 1, 1, 1, 2, 7, 9, 6, 1, 7, 7, 7, 9, 3, 5];
const invalid5 = [5, 3, 8, 2, 0, 1, 9, 7, 7, 2, 8, 8, 3, 8, 5, 4];

// Can be either valid or invalid
const mystery1 = [3, 4, 4, 8, 0, 1, 9, 6, 8, 3, 0, 5, 4, 1, 4];
const mystery2 = [5, 4, 6, 6, 1, 0, 0, 8, 6, 1, 6, 2, 0, 2, 3, 9];
const mystery3 = [6, 0, 1, 1, 3, 7, 7, 0, 2, 0, 9, 6, 2, 6, 5, 6, 2, 0, 3];
const mystery4 = [4, 9, 2, 9, 8, 7, 7, 1, 6, 9, 2, 1, 7, 0, 9, 3];
const mystery5 = [4, 9, 1, 3, 5, 4, 0, 4, 6, 3, 0, 7, 2, 5, 2, 3];

// An array of all the arrays above
const batch = [valid1, valid2, valid3, valid4, valid5, invalid1, invalid2, invalid3, invalid4, invalid5, mystery1, mystery2, mystery3, mystery4, mystery5];


// Add your functions below:
//console.log(batch)

const validateCred = (arr) => {
  const arrLength = arr.length - 1;
  let total = 0;
  
  for (let i = arrLength; i >= 0; i--) {
  	let currVal = arr[i];
    
    if(arrLength - i % 2 === 1) {
      currVal *= 2;
      
      if(currVal > 9) {
        currVal -= 9;
      }
    }
    
    totalVal += currVal;
  }
  
  return totalVal % 10 === 0;
}

Anyway, as you can see I’ve completed the project now.

And you were extremely helpful. I got new insight on the .indexOf() method. Thanks a bunch.:slightly_smiling_face:

1 Like

Ahh, right!

Glad you figured it out. :slight_smile: Let us know if there’s anything else we can help with . :+1:

1 Like

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.