Skip to content

Instantly share code, notes, and snippets.

@emmabostian
Created March 19, 2020 18:10
Show Gist options
  • Save emmabostian/d98f7ae8d7a7590f81400ce8c8cdef73 to your computer and use it in GitHub Desktop.
Save emmabostian/d98f7ae8d7a7590f81400ce8c8cdef73 to your computer and use it in GitHub Desktop.
function fearNotLetter(str) {
let letters = str.split('');
let startingCharCode = letters[0].charCodeAt(0);
let endingCharCode = letters[letters.length-1].charCodeAt(0);
// Find the current sum of char codes
let currentTotalCharCodes = letters.reduce((total, letter) => total += letter.charCodeAt(0), 0);
// Find the expected sum of char codes
let expectedTotalCharCodes = 0;
for(let i = startingCharCode; i <= endingCharCode; i++) {
expectedTotalCharCodes += i;
}
// Calculate the difference to find the missing letter
let missingLetterCharCode = expectedTotalCharCodes - currentTotalCharCodes;
return missingLetterCharCode === 0 ? undefined : String.fromCharCode(missingLetterCharCode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment