var arr = ['a', 'b', 'd', 'e', 'f', 'g'];
function findMissingLetterWithIndex(arr) {
for (let i = 0; i < arr.length - 1; i++) {
const currentCharCode = arr[i].charCodeAt(0);
const nextCharCode = arr[i + 1].charCodeAt(0);
if (nextCharCode - currentCharCode > 1) {
return {
missingLetter: String.fromCharCode(currentCharCode + 1),
missingIndex: i + 1
};
}
}
}
const result = findMissingLetterWithIndex(arr);
console.log("Missing letter:", result.missingLetter); // Output: Missing letter: c
console.log("Missing index:", result.missingIndex); // Output: Missing index: 2
The missing letter is ‘c’, and its index is 2.