Finding missing letter in a string - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We have a string of length m that contains first m letters of the English alphabets, but somehow, one element went missing from the string. So, now the string contains,

m-1 letters

We are required to write a function that takes in one such string and returns the missing element from the string

Example

Following is the code −

const str = "acdghfbekj";
const missingCharacter = str => {
   // to make the function more consistent
   const s = str.toLowerCase();
   for(let i = 97; ; i++){
      if(s.includes(String.fromCharCode(i))){
         continue;
      };
      return String.fromCharCode(i);
   };
   return false;
};
console.log(missingCharacter(str));

Output

Following is the output in the console −

i
raja
Published on 16-Sep-2020 13:39:15
Advertisements