-1

here's the problem: I want to be able to type a word and the result will display all the alphabet letters which are not used in that word. Example1: after you input "ABCDEFGHIJKL", the result is "MNOPQRSTUVWXYZ". Example2: after you input "UNCOPYRIGHTABLE", the result is "DFJKMQSVWXZ".

I searched everything and couldn't find a way to do this. Please help, thanks in advance!

Niteb
  • 1

3 Answers3

0

Using String.prototype.includes(), Array.prototype.filter(), String.prototype.toLowerCase() and spread syntax.

The solution only works if all characters in the word are all lowercase or all uppercase.

function alphabetDifference(word) {
  return word === word.toLowerCase()
    ? [..."abcdefghijklmnopqrstuvwxyz"]
        .filter((ch) => !word.includes(ch))
        .join("")
    : [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
        .filter((ch) => !word.includes(ch))
        .join("");
}

console.log(alphabetDifference("ABCDEFGHIJKL"),"MNOPQRSTUVWXYZ");
console.log(alphabetDifference( "UNCOPYRIGHTABLE"),"DFJKMQSVWXZ");
console.log(alphabetDifference("abcd"));
console.log(alphabetDifference("EHJKL"));

This works for all lowercase, uppercase as well as mixed case.

function alphabetDifference(word) {
  if (word === word.toLowerCase()) {
    return [..."abcdefghijklmnopqrstuvwxyz"]
      .filter((ch) => !word.includes(ch))
      .join("");
  } else if (word === word.toUpperCase()) {
    return [..."ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
      .filter((ch) => !word.includes(ch))
      .join("");
  } else {
    return [..."abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
      .filter((ch) => !word.includes(ch))
      .join("");
  }
}

console.log(alphabetDifference("aBcD"));
console.log(alphabetDifference("EHJKL"));
0

There's probably a fancy way to do this with Regex or something, but I'm not familiar with Regex so I'll just give a very primitive example.

const alphabet = "abcdefghijklmnopqrstuvwxyz";

function findAllLettersNotIn(input){
    input = input.toLowerCase();
    let toReturn = alphabet.split("");
    for(let i = 0;i < input.length;i++){
        if(toReturn.indexOf(input.charAt(i)) >= 0){
            toReturn.splice(toReturn.indexOf(input.charAt(i)),1);
        }
    }
    return(toReturn.join(""));
}

console.log(findAllLettersNotIn("Just a test string."));
console.log(findAllLettersNotIn("ABCDEFGHIJKL"));
console.log(findAllLettersNotIn("UNCOPYRIGHTABLE"));

My solution will return a string with all the letters lowercase. If this is not desirable you can easily change that with toUpperCase() if you'd like. My solution also supports non-letter characters, and will simply ignore them if they exist.

OOPS Studio
  • 732
  • 1
  • 8
  • 25
-1

You can use the string.slice() to get the letters into an array, than use this code:

function arr_diff (a1, a2) {

    var a = [], diff = [];

    for (var i = 0; i < a1.length; i++) {
        a[a1[i]] = true;
    }

    for (var i = 0; i < a2.length; i++) {
        if (a[a2[i]]) {
            delete a[a2[i]];
        } else {
            a[a2[i]] = true;
        }
    }

    for (var k in a) {
        diff.push(k);
    }

    return diff;
}

To get the difference between the two arrays. You can read more about this at: here

Laczkó Örs
  • 1,082
  • 1
  • 18
  • 38