'Searching a string for missing letters of the alphabet in javascript

I'm working on a bit of code that will search through a string and return any letters of the alphabet that are missing. This is what I have:

function findWhatsMissing(s){
    var a = "abcdefghijklmnopqrstuvwxyz";
    //remove special characters
    s.replace(/[^a-zA-Z]/g, "");
    s = s.toLowerCase();
    //array to hold search results
    var hits = [];

    //loop through each letter in string
    for (var i = 0; i < a.length; i++) {
        var j = 0;
        //if no matches are found, push to array
        if (a[i] !== s[j]) {
                hits.push(a[i]);
        }
        else {
            j++;
        }
    }
    //log array to console
    console.log(hits);
}

But using the test case: findWhatsMissing("d a b c");

Results in all the letters before d being added to the missing array.

Any help would be greatly appreciated.



Solution 1:[1]

Inside your loop, you can use indexOf() to see if the letter exists in your input. Something like this would work:

for (var i = 0; i < a.length; i++) {
    if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
}

Hope that helps! You can see it working in this JS Fiddle: https://jsfiddle.net/573jatx1/1/

Solution 2:[2]

As Adam Konieska says. Something like this will work:

   function findWhatsMissing(s) {
        var a = "abcdefghijklmnopqrstuvwxyz";
        s = s.toLowerCase();
        var hits = [];
        for (var i = 0; i < a.length; i++) {
            if(s.indexOf(a[i]) == -1) { hits.push(a[i]); }
        }

       console.log(hits);
    }

    findWhatsMissing("d a b c");

Solution 3:[3]

Can use Array.prototype.filter() and within each loop check string using indexOf()

function findWhatsMissing(s){
    var a = "abcdefghijklmnopqrstuvwxyz";
    //remove special characters
    s = s.replace(/[^a-zA-Z]/g, "");
    s = s.toLowerCase();
    return a.split('').filter(function(letter){
      return s.indexOf(letter) === -1;
    });
}

alert( findWhatsMissing('d f v'))

Solution 4:[4]

You can use indexOf:

function findWhatsMissing(s){
    var a = "abcdefghijklmnopqrstuvwxyz";
    //remove special characters
    s = s.replace(/[^a-zA-Z]/g, "");
    s = s.toLowerCase();
    //array to hold search results
    var hits = [];

    //loop through each letter in string
    for (var i = 0; i < a.length; i++) {
        //if no matches are found, push to array
        if (s.indexOf(a[i]) == -1) {
                hits.push(a[i]);
        }
    }
    //log array to console
    return hits;
}
alert(JSON.stringify(findWhatsMissing(' d a b c ')));

or two for loops:

function findWhatsMissing(s){
  var a = "abcdefghijklmnopqrstuvwxyz";
  //remove special characters
  s = s.replace(/[^a-zA-Z]/g, "");
  s = s.toLowerCase();
  //array to hold search results
  var hits = [];

  //loop through each letter in string
  for (var i = 0; i < a.length; i++) {
    //if no matches are found, push to array
    var found = false;
    for (var j = 0; j < s.length; j++) {
      if (s[j] == a[i]) {
        found = true;
        break;
      }
    }
    if (!found) {
      hits.push(a[i]);
    }
  }
  //log array to console
  return hits;
}
alert(JSON.stringify(findWhatsMissing(' d a b c ')));

Solution 5:[5]

function missingchar(str) {
let arr = new Array(26);
let mis_str = '';
Acharcode = 'A'.charCodeAt();
Zcharcode = 'Z'.charCodeAt();
for (i = Acharcode; i <= Zcharcode; i++) {
    arr[i] = String.fromCharCode(i).toLowerCase();
}
arr.map((item, index) => {
    if (str.indexOf(item) == -1) {
        mis_str += item;
    }
})
return mis_str;

}

console.log(missingchar('satya'));

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam Konieska
Solution 2
Solution 3
Solution 4
Solution 5 Satyendra Singh