Created
March 19, 2020 18:10
-
-
Save emmabostian/d98f7ae8d7a7590f81400ce8c8cdef73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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