Missing letters - Not returning undefined

Tell us what’s happening:
Hi everyone,

I believe that I’ve already solved this algorithm challenge. It asks me to return undefined if there is no letter missing, however, when I continue to do so, it returns “Can’t convert undefined to object”. I hope I’m not the one missing anything but I’d appreciate any assitance.

Thanks

Your code so far


function fearNotLetter(str) {
  for( let i = 0; i < str.length; i++ ){
    if(i + 1 !== str.length + 1){
      if( String.charCodeAt(str[i + 1]) - String.charCodeAt(str[i]) !== 1 ){
        return String.fromCharCode( String.charCodeAt(str[i + 1]) - 1 );
      }
    }
  }
  return undefined;
}

fearNotLetter("abce");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/missing-letters

You have other mistakes in your code that you need to correct. If your correct them, your “return undefined” should work :slight_smile:

This is not the right syntax.
Don’t hesitate if you need more help!

I got it! I was using charCodeAt the wrong way… Thank you so much!

1 Like

here is another approach for solving the problem:

function fearNotLetter(str) {
  // create an array of english letters
  let alpha= ("abcdefghijklmnopqrstuvwxyz").split("");
  //index of the first letter in the given string
  let i=alpha.indexOf(str[0]);
  for(let j=0;j<str.length;j++){
    // start comparing the str with alphapatic letter
    if(alpha[i+j]!=str[j])
    // return the missing letter
    return alpha[i+j];
  }
  return undefined;
}