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
map
to get an array of the unicode numbers (using charCodeAt) of the letters in the input array and then usefind
to find the missing character code. - You can also use
filter
as 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
start
and assigned it the ASCII code of the first letter of the input array. - Declare a variable
missingCharCode
and assigned it the result of calling themap
method on the input array. The callback function passed to themap
method takes in a letter and returns the ASCII code of that letter. - Call the
find
method on the result of themap
method. The callback function passed to thefind
method 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
find
method returns a truthy value, we return the letter that is one less than the current letter. - If the
find
method 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
missingCharCode
and assigned it the result of calling thefilter
method on the input array. - The callback function passed to the
filter
method 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
filter
method returns an array with a length greater than 0, we return the letter that is one less than the current letter. - If the
filter
method 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
start
and assigned it the ASCII code of the first letter of the input array. - Declare a variable
missingCharCode
and assigned it the result of calling thereduce
method on the input array. The callback function passed to thereduce
method 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 thestart
variable. It then returns the accumulator. - If the
reduce
method returns a truthy value, we return the letter that is one less than the current letter. - If the
reduce
method 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');
});