1

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.

9
  • 1
    sounds like you are going passed the array index? are you doing a Caesar cipher? Commented Feb 16, 2018 at 13:53
  • 2
    What is the alphabetArr value? Commented Feb 16, 2018 at 13:53
  • do var addThirteen = (i + 13) % alphabetArr.length; 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. 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. Commented Feb 16, 2018 at 13:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.