Missing letters question

Tell us what’s happening:
As you can see, I am having problems with the for loop on line 17. My guess is that I am not using afterFirst[i] in strArr properly, but am not sure.

Your code so far


function fearNotLetter(str) {
 var counter = 0;
 var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var strArr = str.split("");
var last = strArr[strArr.length-1];
var first = strArr[0];
var loopCounter=0;
console.log("strArr is " + strArr + " and the last letter is " + last);
console.log("first letter is " + first);
var afterFirst = alphabet.slice(alphabet.indexOf(first));
console.log("afterFirst is " + afterFirst);
var lettersAfterLast = afterFirst.slice(afterFirst.indexOf(last)).length-1;
console.log("letterAfterLast " + lettersAfterLast);
var afterLast = afterFirst.splice(afterFirst.indexOf(last)+1,lettersAfterLast+1);
console.log("afterLast is " + afterLast);
console.log("!!! the new afterFirst is " + afterFirst);
for (let i=0;i<afterFirst.length;i++) {
    if (afterFirst[i] in strArr ) {
      loopCounter++;
    }  else {
      return afterFirst[i];
    }
    
console.log("counter is " + loopCounter + " and length is " + afterFirst.length);
if (loopCounter == afterFirst.length) {
  return undefined;
}
   
  }
}

fearNotLetter("bcdf");

Your browser information:

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

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

Instead of hard coding the alphabets. I recommend you use


These two methods convert letter into ascii code which is an integer. Then you can find missing integer in a range to solve this challenge.

I played around a bit and got it solved. Is it any problem that I just copied an array with the alphabet that I found online? Or I guess if I wrote a string and the just used split to get the array? Here is my code:

function fearNotLetter(str) {
 var counter = 0;
 var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
var strArr = str.split("");
var last = strArr[strArr.length-1];
var first = strArr[0];
var loopCounter=0;
console.log("strArr is " + strArr + " and the last letter is " + last);
console.log("first letter is " + first);
var afterFirst = alphabet.slice(alphabet.indexOf(first));
console.log("afterFirst is " + afterFirst);
var lettersAfterLast = afterFirst.slice(afterFirst.indexOf(last)).length-1;
console.log("letterAfterLast " + lettersAfterLast);
var afterLast = afterFirst.splice(afterFirst.indexOf(last)+1,lettersAfterLast+1);
console.log("afterLast is " + afterLast);
console.log("!!! the new afterFirst is " + afterFirst);
for (let i=0;i<afterFirst.length;i++) {
    if (strArr.includes(afterFirst[i]) ) {
      //loopCounter++;
    }  else {
      return afterFirst[i];
    }
    
//console.log("counter is " + loopCounter + " and length is " + afterFirst.length);
if (loopCounter == afterFirst.length) {
  return undefined;
}
   
  }
}

fearNotLetter("bcdf");

The above is the equivalent of writing:

var alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

OR

var alphabet = [...'abcdefghijklmnopqrstuvwxyz'];

Also, the above is not necessary, because if you make it all the way through the for loop without returning anything, you know you can return undefined, so just return undefined after the for loop.

The above can be simplified as:

if (!strArr.includes(afterFirst[i])) {
  return afterFirst[i];
}