I'm not entirely sure how to make this function work with capital letters. Currently, an array of all capital characters returns undefined when passed and processed in the "for loop".
This function takes an array of lowercase string only characters and returns the first missing character, but I also need it to work with caps.
I was able to solve this issue with around 10 extra lines, but It seems like there must be a better approach!
const alph = "abcdefghijklmnopqrstuvwxyz";
const findMissingLetter = (arr) => {
if(arr.length===0)
return -1;
let start = alph.indexOf(arr[0]);
for(let i=0; i<arr.length; i++) {
if(arr[i] !== alph[start+i]) {
return alph[start+i];
}
}
return -1
}
console.log(findMissingLetter([]),"-1?"); //ok
console.log(findMissingLetter(['a']),"-1?"); //ok
console.log(findMissingLetter(['b']),"-1?"); //ok
console.log(findMissingLetter(['a','b']),"-1?"); //ok
console.log(findMissingLetter(['a','c']),"b?"); //ok
console.log(findMissingLetter(['b','c','e','f']),"d?"); //ok
console.log(findMissingLetter(['B','C','E','F']),"D?"); //returns undefined
//console.log(findMissingLetter(['B','c','E','f']),"D?"); //not necessary for this solution!
Hope you can help, Thank you!