Q:

Why doesn't .charCodeAt() ensure that you can find the missing letter in this Javascript Algorithm?

Question
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.

function fearNotLetter(str) {

  let testArr = [0, str.charCodeAt(0)];

  str.split('').forEach((item, index)=>{
      testArr[0] = testArr[1];
      testArr[1] = item.charCodeAt(0);
      if(testArr[1] != (testArr[0] + 1)){
        return str[index];
      }
    })

  return undefined;
}

console.log(fearNotLetter("abce"));
console.log(fearNotLetter("stvwx"));

A:

Try this:

function fearNotLetter(str) {
  let letter;
  let testArr = [0, str.charCodeAt(0)];
  str.split('').forEach((item, index)=>{
      testArr[0] = testArr[1];
      testArr[1] = item.charCodeAt(0);
      if(testArr[1] != (testArr[0] + 1)){
        letter = index ? str[index]: undefined;
      }
    })

  return letter;
}

console.log(fearNotLetter("abce"));
console.log(fearNotLetter("stvwx"));
console.log(fearNotLetter("abcdefg"));

