Skip to content

Instantly share code, notes, and snippets.

@flesch
Created July 24, 2019 17:52
Show Gist options
  • Save flesch/3f70ce89fa27e465952f70b1f6f41cc1 to your computer and use it in GitHub Desktop.
Save flesch/3f70ce89fa27e465952f70b1f6f41cc1 to your computer and use it in GitHub Desktop.
JavaScript Interview Code Challenge Solution
// A "pangram" is a sentence that includes every letter of the alphabet.
// Write a function that will return what letters of the alphabet are
// missing from a sentence (thus meaning it is not a pangram).
// "A quick brown fox jumps over the lazy dog" includes every letter, returning ""
// "Lions, and tigers, and bears, oh my!" does not include every letter, returning "cfjkpquvwxz"
function findMissingLetters(str) {
const alphabet = 'abcdefghijklmnopqrstuvwzyz';
if (str.length) {
const reducer = (memo, letter) => {
if (!str.includes(letter) && !memo.includes(letter)) {
return [...memo, letter];
}
return memo;
};
return alphabet
.split('')
.reduce(reducer, [])
.join('');
}
return alphabet;
}
console.log(findMissingLetters('A quick brown fox jumps over the lazy dog')); // ==> ""
console.log(findMissingLetters('A slow yellow fox crawls under the proactive dog')); // ==> "bjkmqz"
console.log(findMissingLetters('Lions, and tigers, and bears, oh my!')); // ==> "cfjkpquvwxz"
console.log(findMissingLetters('')); // ==> "abcdefghijklmnopqrstuvwxyz"
@flesch
Copy link
Author

flesch commented Nov 29, 2024

function findMissingLetters(str) {
  // const alphabet = Array.from({ length: 26 }, (_, i) => {
  //   return String.fromCharCode(97 + i);
  // });

  const strLetters = new Set(str.toLowerCase().match(/[a-z]/g));

  return [..."abcdefghijklmnopqrstuvwxyz"].filter((letter) => {
    return !strLetters.has(letter);
  }).join("");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment