Skip to content

Instantly share code, notes, and snippets.

@flesch
Created July 24, 2019 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment