Missing letters issue

Missing letters issue
0

#1
function fearNotLetter(str) {
  
 for(x=str.length - 1; x >= 0; x--){
    
    if(str.charCodeAt(x) - str.charCodeAt(x-1) === 1){
       return undefined;
     } 
    
    else if((str.charCodeAt(x) - str.charCodeAt(x-1)) > 1){
      return String.fromCharCode(str.charCodeAt(x) - 1);
      
    }
  }
}

  fearNotLetter("abcdefghjklmno");

So my code is passing 3/4 of the tests. It just can’t pass the one provided (abcdefghjklmno). I feel like its something obvious, but have been banging my head against a wall, trying to find the issue.


#2

Three hints:

  1. for loops execute the code contained within on each iteration, not just the last one.
  2. A function can return only one value.
  3. Commenting out/deleting a single line allows your code to pass the tests.

#3

Thanks for the first hint. I Just removed the first If statement, and returned undefined, outside of the For loop, and it works.


#4

You do not actually have to use return undefined; out side the for loop. By default, a function will return undefined if nothing else is returned. So, the following would work:

function fearNotLetter(str) {
 for(x=str.length - 1; x >= 0; x--)
    if((str.charCodeAt(x) - str.charCodeAt(x-1)) > 1)
      return String.fromCharCode(str.charCodeAt(x) - 1);
}

#5

good to know randell, thanks all