Created
July 24, 2019 17:52
-
-
Save flesch/3f70ce89fa27e465952f70b1f6f41cc1 to your computer and use it in GitHub Desktop.
JavaScript Interview Code Challenge Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |
Author
flesch
commented
Nov 29, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment