Missing letters - Need feedback/ help

Tell us what’s happening:

I took a possibly too complicated approach, but it was the only one that came to mind without cheating and checking other answers.

I wanted to find out the missing letter by calculating the difference between the “strCount” (which is the Ascii numbers sum of all the given letters) and the “lettersCount” (which is the Ascii numbers sum of the correct letters sequence). The resulted number could be transformed in the missing letter. However I’m having trouble returning undefined.

It feels complicated to even explain the code, which is probably a sign it’s not good.

I would greatly appreciate some feedback on my code. Please share any thoughts.

Your code so far


function fearNotLetter(str) {

  var letters = 'abcdefghijklmnopqrstuvwxyz';
  var strCount = 0;
  var lettersCount = 0;

  //extracting the Ascii of the first letter in str
  var firstLetterNum = str.charCodeAt(0);

  //transforming the Ascii into the letter
  var firstLetter = String.fromCharCode(firstLetterNum);

  var missingLetterUnicode = 0;

  //finding out the Ascii sum of all letters in the given string
  for (var i = 0; i < str.length; i++) {
    strCount += str.charCodeAt(i);
  }

  //findint out the Ascii sum of all the letters in the given string + the missing one
  for (var j = letters.indexOf(firstLetter); j < letters.indexOf(firstLetter) + str.length + 1; j++) {
    lettersCount += letters.charCodeAt(j);
  }

  if ((missingLetterUnicode = lettersCount - strCount) != 0) {
    return String.fromCharCode(missingLetterUnicode);
  }
  else if (strCount === lettersCount) {
    return undefined;
  }
}

console.log(fearNotLetter("abcdefghijklmnopqrstuvwxyz"));

Your browser information:

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

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

  //findint out the Ascii sum of all the letters in the given string + the missing one
  for (var j = letters.indexOf(firstLetter); j < letters.indexOf(firstLetter) + str.length + 1; j++) {
    lettersCount += letters.charCodeAt(j);
  }

The is that this loop assumes that a letter will be missing, so it always attempts to look at a letter “after” the last one in str. If the complete alphabet is in str, then it attempts to do

lettersCount += letters.charCodeAt(26);

But there is no letters[26], so it is undefined. This makes lettersCount become NaN. Then strCount === lettersCount is false.

feedback: wouldn’t it be easier to do str[0]?

I was using the tool Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code to check if everything is fine… well, your lettersCount get a value of NaN at one point (when j has value of 26)

You don’t need to do anything special to make the function return undefined, if it doesn’t return anything, than it returns undefined

feedback: wouldn’t it be easier to do str[0] ?

Yes, you are right.

I was using the tool Online JavaScript Compiler, Visual Debugger, and AI Tutor - Learn JavaScript programming by visualizing code to check if everything is fine… well, your lettersCount get a value of NaN at one point (when j has value of 26 )

I didn’t know about this tool, thank you!

You don’t need to do anything special to make the function return undefined , if it doesn’t return anything, than it returns undefined

I deleted return undefined and it seemed to have no effect

The problem is surely with j reaching a value of 26, as @ArielLeslie also pointed out.

Thank you! I will find a way to get around that.