Missing letter from strings in JavaScript


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

Therefore, let’s write the code for this function −

Example

The code for this will be −

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

The output in the console will be −

i

Updated on: 17-Oct-2020

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements