Missing letters - Works only for some string and not for any case

Tell us what’s happening:
Hello. I was trying to solve this problem and that was the first way that came into my mind. I don’t know if it is a good way to solve it but anyway, that is not my problem. Something strange is happening with my code. In some cases it works, and in some others it doesn’t. The strange is that I have not written something to specialize some cases. I can’t find what is happening. Please, if you can see what is wrong, I would like to make it clear.

Your code so far


function fearNotLetter(str) {
  const a="abcdefghijklmnopqrstuvwxyz";
  if(a.indexOf(str)>=0){
    return undefined;
  }
  else{
  var con = a.slice(a.indexOf(str[0]),str.length+1);
  for(let i=0;i<str.length;i++){
          if(str.indexOf(con[i])<0){
            return con[i];
                 }
      }
      } 
}
fearNotLetter("abce");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36.

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

So it’s a matter of how you’re using slice(). As long as the first letter in str is ‘a’, you’re doing fine. Otherwise, it’s failing. Why? Because of the signature of slice(...).

You’re trying to tell slice() “start at this index, and slice this many characters”. That’s not how slice() works. Instead, you need to define a starting index AND an ending index. What do you think the ending index ought to be?

function fearNotLetter(str) {
  // the complete lowercase alpha.
  const a="abcdefghijklmnopqrstuvwxyz";

  // if the string is in good alpha order, return undefined.
  if(a.indexOf(str)>=0){
    return undefined;
  } else {
    // in this case, we're missing a letter. Check them all.
    // in order to slice the complete alpha string, we need a start and end index.
    var startAt = a.indexOf(str[0]),
      endAt = ????????,  // What do you think this ought to be?
      con = a.slice(startAt, endAt);

      // go letter by letter through the strings...
      for(let i=0;i<str.length;i++){
        // Just to be sure, let's debug what values we're comparing
        console.log("str[i]: ",str[i], ", con[i]: ",con[i]);

        // Are they the different?
        if(str[i]!== con[i]){
          // This is the mismatch. Let's send it back!
          return con[i];
        }
      }
    } 
}

Also note, I am a firm believer in over-commenting code. Document as you go, it makes debugging much easier!

I chose to end it at “str.length + 1”, because there is always one letter that is missing. What makes it different to some cases? If, I would chose something else for it maybe it would be the index of the last minus the index of the first? I want to count how many they are anyway, right?

the ending point should be the starting point PLUS the length you want.

What you’re doing, currently, is starting at (for example) index 18 (the letter ‘s’) and ending at index 5 (the letter ‘f’). Is that your intent?

I am very surprised I just realized that the ending is set as the ending index, I thought that it was the amount of the elements I was taking from the ‘a’ string. That is why it seemed so weird to me. Thank you so much! I just editted it and it works fine!

1 Like

The problem is that it was so close, and it could also be close at other codes and using it wrong without realizing it.

Glad to help. Also, the MDN docs are your friendz. :wink:

The only way I found it was throwing those console.log() statements. Well, and some in a few other places (for example, logging con before I even entered the loop showed that there was a problem.

Yes, I tried that and I saw the issues but I was so sure that the slice was right and I thought that there was a logical issue somewhere else but everything seemed pretty logical to me (!) You are so right about the MDN docs, too. Thanks :blush: