In the last section, we created a function calledfindMissingLetter that takes in an array of consecutive (increasing) letters as input and returns the missing letter in the array.
I want to do that same thing using high order array methods. It is possible to do it using map, filter, reduce, or forEach.
/**
* Returns the missing letter in an array of consecutive letters.
* @param {string[]} arr - An array of consecutive letters.
* @returns {string} - The missing letter.
*/
function findMissingLetter(arr: string[]): string;findMissingLetter(['a', 'b', 'c', 'd', 'f']); // => "e"
findMissingLetter(['O', 'Q', 'R', 'S']); // => "P"
findMissingLetter(['t', 'u', 'v', 'w', 'x', 'z']); // => "y"- The input array will always contain at least two letters
- The input array will only contain letters in one case (lower or upper)
- Use only high order array methods. No for loops allowed.
- You can use
mapto get an array of the unicode numbers (using charCodeAt) of the letters in the input array and then usefindto find the missing character code. - You can also use
filteras well asreduce
Click For Solution 1
This solution uses the map and find methods.
function findMissingLetter(arr) {
let start = arr[0].charCodeAt(0);
const missingCharCode = arr
.slice(1)
.map((char) => char.charCodeAt(0))
.find((current) => {
if (current - start > 1) {
return true;
}
start = current;
return false;
});
return missingCharCode ? String.fromCharCode(missingCharCode - 1) : '';
}- Declare a variable
startand assigned it the ASCII code of the first letter of the input array. - Declare a variable
missingCharCodeand assigned it the result of calling themapmethod on the input array. The callback function passed to themapmethod takes in a letter and returns the ASCII code of that letter. - Call the
findmethod on the result of themapmethod. The callback function passed to thefindmethod takes in the current ASCII code and checks if the current ASCII code minus the previous ASCII code is greater than 1. If it is, it returnstrue. If it is not, it returnsfalse. - If the
findmethod returns a truthy value, we return the letter that is one less than the current letter. - If the
findmethod returns a falsy value, we return an empty string.
Click For Solution 2
This solution uses the filter method.
function findMissingLetter(arr) {
const missingCharCode = arr.filter((char, index) => {
if (index === 0) return false;
const prevCharCode = arr[index - 1].charCodeAt(0);
const currentCharCode = char.charCodeAt(0);
return currentCharCode - prevCharCode > 1;
})[0];
return missingCharCode
? String.fromCharCode(missingCharCode.charCodeAt(0) - 1)
: '';
}- Declare a variable
missingCharCodeand assigned it the result of calling thefiltermethod on the input array. - The callback function passed to the
filtermethod takes in a letter and its index and checks if the current letter is the first letter in the array. If it is, it returnsfalse. If it is not, it checks if the current letter's ASCII code minus the previous letter's ASCII code is greater than 1. If it is, it returnstrue. If it is not, it returnsfalse. - If the
filtermethod returns an array with a length greater than 0, we return the letter that is one less than the current letter. - If the
filtermethod returns an array with a length of 0, we return an empty string.
Click For Solution 3
This solution uses the reduce method.
function findMissingLetter(arr) {
let start = arr[0].charCodeAt(0);
const missingCharCode = arr.reduce((missing, char) => {
const current = char.charCodeAt(0);
if (current - start > 1 && missing === null) {
missing = start + 1;
}
start = current;
return missing;
}, null);
return missingCharCode ? String.fromCharCode(missingCharCode) : '';
}- Declare a variable
startand assigned it the ASCII code of the first letter of the input array. - Declare a variable
missingCharCodeand assigned it the result of calling thereducemethod on the input array. The callback function passed to thereducemethod takes in an accumulator and a letter. The accumulator is the missing ASCII code. The callback function checks if the current ASCII code minus the previous ASCII code is greater than 1 and if the accumulator isnull. If it is, it assigns the missing ASCII code to the accumulator. It then assigns the current ASCII code to thestartvariable. It then returns the accumulator. - If the
reducemethod returns a truthy value, we return the letter that is one less than the current letter. - If the
reducemethod returns a falsy value, we return an empty string.
test('Find Missing Letter', () => {
expect(findMissingLetter(['a', 'b', 'c', 'e'])).toBe('d');
expect(findMissingLetter(['X', 'Z'])).toBe('Y');
expect(findMissingLetter(['m', 'n', 'o', 'q', 'r'])).toBe('p');
expect(findMissingLetter(['F', 'G', 'H', 'J'])).toBe('I');
});