I'm facing an issue with the splice function. Here is my code snippet:
const findMissingLetter=(array)=>{
let result, alphabet = "abcdefghijklmnopqrstuvwxyz";
if(array[0]===array[0].toUpperCase()) alphabet = alphabet.toUpperCase();
let start = alphabet.split('').indexOf(array[0]);
return alphabet.split('').splice(start,start+array.length+1);
}
The purpose of this function is to identify a missing letter in the alphabet sequence and return it.
The argument provided will consist of either lowercase or uppercase letters only. The problem arises when I apply this code to the following arrays:
['a','b','c','d','f']
- it works correctly, returning ['a', 'b', 'c', 'd', 'e', 'f']
However, if uppercase letters are used:
['O','Q','R','S']
- the output is
['O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
.
const findMissingLetter = (array) => {
let result, alphabet = "abcdefghijklmnopqrstuvwxyz";
if (array[0] === array[0].toUpperCase()) alphabet = alphabet.toUpperCase();
let start = alphabet.split('').indexOf(array[0]);
return alphabet.split('').splice(start, start + array.length + 1);
}
console.log(findMissingLetter(['a','b','c','d','f']));
console.log(findMissingLetter(['O','Q','R','S']));
What could be causing this issue?