Javascript - Use Variable as Array Index - Stack Overflow

admin2025-04-18  0

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.

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.

Share Improve this question asked Feb 16, 2018 at 13:51 user9109814user9109814 1751 gold badge4 silver badges14 bronze badges 9
  • 1 sounds like you are going passed the array index? are you doing a Caesar cipher? – Daniel A. White Commented Feb 16, 2018 at 13:53
  • 2 What is the alphabetArr value? – zelda11 Commented Feb 16, 2018 at 13:53
  • do var addThirteen = (i + 13) % alphabetArr.length; – Daniel A. White Commented Feb 16, 2018 at 13:53
  • 2 Why parseInt(13)? And why parseInt(i)? i is initialized with 0 (a number) and then incremented. It is always a number. – axiac Commented Feb 16, 2018 at 13:54
  • 2 The problem is not that you're not using the variable as index, the problem is that the index doesn't exist. – deceze Commented Feb 16, 2018 at 13:55
 |  Show 4 more ments

2 Answers 2

Reset to default 2

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 ++) {

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, unment
 * 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);
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744969176a277383.html

最新回复(0)