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/