user9109814
user9109814

Reputation: 175

Javascript - Use Variable as Array Index

Trying to use a variable as an array index. Variable addThirteen simply takes i and adds 13 to it. I need to use that result as the array index. Please see my code:

for (var i = 0; i < alphabetArr.length; i ++) {
  var addThirteen = (parseInt(i) + parseInt(13));
  var newCipher = new cipher(alphabetArr[i].letter, alphabetArr[addThirteen].letter);
}

This results in:

TypeError: undefined is not an object (evaluating 'alphabetArr[addThirteen].letter')

Appreciate any help.

Upvotes: 1

Views: 10544

Answers (2)

theAlexandrian
theAlexandrian

Reputation: 908

The result you are getting is really depending on what is the alphabetArr variable.

  • The following is an example where alphabetArr is just an array of strings.

const alphabetArr = "abcdefghejklmnopqrstuvwxyz".split('');

/* If you want to see what alphabetArr really is, uncomment
 * the following line an run. (copy snippet to answer to do it)
 * The result will be ["a", "b", "c", ..., "z"].
 */
// console.log(alphabetArr);

/* we will always get undefined here because alphabet[i]
 * is not an object having the property letter.
 * Instead, it is just a string.
 */
for (var i = 0; i < alphabetArr.length; i++) {
  console.log(alphabetArr[i].letter);
}

This example gives only undefined because of alphabetArr[i] not being an object whith the property letter but being a string.

  • Now we will try to see what happens if alphabetArr is an array of objects each having the property letter

const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split('')
  .map(letter => ({letter: letter}));
  
// console.log(alphabetArr);
// result: [ { letter: "a" } , ... , { letter: "z" } ]

/* Here everything works fine until we reach
 * the index 13, where the output is m z
 */
for (var i = 0; i < alphabetArr.length; i++) {
  // Note that I am not parsing. It's because it is not necessary.
  var addThirteen = i + 13;
  console.log(alphabetArr[i].letter, alphabetArr[addThirteen].letter);
}

As you see, now we get something, but still, at a certain point, we get the thrown error. This is because the for loop is looping over the entire array. So when i is 14, addThirteen will be 27. But the length of the array is 26, so alphabetArr[27] is undefined.

  • Now that we understood the problem, let us solve it.

const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split('')
  .map(letter => ({letter: letter}));

for (var i = 0; i < alphabetArr.length - 13; i++) {
  // let's skip the addThirteen variable declaration.
  console.log(alphabetArr[i].letter, alphabetArr[i + 13].letter);
}

Yes! The solution is to loop over the array minus thirteen, to avoid out of range indexes.

The final code:

const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split('')
  .map(letter => ({letter: letter}));

for (var i = 0; i < alphabetArr.length - 13; i++) {
  var newCipher = new cipher(alphabetArr[i].letter, alphabetArr[i + 13].letter);
}

Upvotes: 1

Jogaklaa
Jogaklaa

Reputation: 46

Try printing out the iteration the for loop is on each time:

console.log(i); console.log(addThirteen);

What seems to be happening here is that when i is alphabetArr.length-13 it is trying to get a character that is outside the array. As it can't do this it throws up the error saying that the letter is undefined, because it is. A solution to this problem is:

for (var i = 0; i < alphabetArr.length - 13; i ++) {

Upvotes: 2

Related Questions