Missing letters - Help Needed

Missing letters - Help Needed
0

#1

Tell us what’s happening:
Hi there,

I’m a bit stuck on the missing letters algorithm. I can’t figure out why the first return statement is not exiting the function and loop. I also don’t understand why my answers for some tests, such as “abce”, are not being accepted even though they are correct.

Cheers!

Your code so far


function fearNotLetter(str) {
  let alph = "abcdefghijklmnopqrstuvwxyz".split("");
  let input = str.split("");
  let i = alph.indexOf(str.charAt(0));

  input.forEach(function(letter) {
    if ( letter != alph[i] ) {
      console.log(alph[i]);
      return alph[i];
    }
    i++;
  });
  return undefined;
}

fearNotLetter("abcdefghjklmno");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

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


#2

When you use a return statement in the callback function of for Each, you not exiting the fearNotLetter function. You are just causing the next iteration of the forEach to start. You should use a regular for loop here instead.

The only return statement that causes your function to exit is the last one, once the forEach has finished iterating over the input array. This means your fearNotletter function returns undefined for any call to it.