admin管理员组文章数量:1414852
I am trying to create a function that takes an array input of consecutive characters in the alphabet and returns the missing letter if there is one (there will only be 1 missing letter and each element in the array will be listed in alphabetical order).
Example inputs:
['a', 'b', 'c', 'e'] -> 'd'
['l', 'n', 'o', 'p'] -> 'm'
['s', 't', 'u', 'w', 'x'] -> 'v'
const findMissingLetter = () => {
const stepOne = (array) => {
for (let i = 0; i < array.length; i++) {
let x = array.charCodeAt(i + 1);
let y = array.charCodeAt(i);
if ((x - y) != 1) {
return (array.charCodeAt[i] + 1);
}
}
}
}
return findMissingLetter(stepOne.fromCharCode(array));
What I am attempting to do is loop through each index of the array and convert each character to unicode. If the [i + 1] - [i] elements in the array equal 1, then there is no missing letters. However if it does not equal 1, then I want to return the unicode of [i] + 1 and then through the higher order function convert the unicode output back to the corresponding character in the alphabet.
Can someone please explain what I am doing wrong? I know I am not calling the functions correctly.
Thanks!
I am trying to create a function that takes an array input of consecutive characters in the alphabet and returns the missing letter if there is one (there will only be 1 missing letter and each element in the array will be listed in alphabetical order).
Example inputs:
['a', 'b', 'c', 'e'] -> 'd'
['l', 'n', 'o', 'p'] -> 'm'
['s', 't', 'u', 'w', 'x'] -> 'v'
const findMissingLetter = () => {
const stepOne = (array) => {
for (let i = 0; i < array.length; i++) {
let x = array.charCodeAt(i + 1);
let y = array.charCodeAt(i);
if ((x - y) != 1) {
return (array.charCodeAt[i] + 1);
}
}
}
}
return findMissingLetter(stepOne.fromCharCode(array));
What I am attempting to do is loop through each index of the array and convert each character to unicode. If the [i + 1] - [i] elements in the array equal 1, then there is no missing letters. However if it does not equal 1, then I want to return the unicode of [i] + 1 and then through the higher order function convert the unicode output back to the corresponding character in the alphabet.
Can someone please explain what I am doing wrong? I know I am not calling the functions correctly.
Thanks!
Share Improve this question edited Nov 4, 2018 at 19:44 hbagley asked Nov 4, 2018 at 19:35 hbagleyhbagley 631 gold badge2 silver badges6 bronze badges 3- "chronological" - did you mean ascending? – Bergi Commented Nov 4, 2018 at 19:43
-
It's
array.charCodeAt(i)
notarray.charCodeAt[i]
– Bergi Commented Nov 4, 2018 at 19:44 -
"I know I am not calling the functions correctly." - can you fix this first, please? Those nested declarations make no sense. You only need a single function. Also, you probably want
String.fromCharCode
notstepOne.fromCharCode
. – Bergi Commented Nov 4, 2018 at 19:45
4 Answers
Reset to default 4The string method .charCodeAt()
doesn't work on arrays. You need to use it on each character, and get the code at position 0 (the default):
const findMissingLetter = (array) => {
// we can skip the 1st letter
for (let i = 1; i < array.length; i++) {
// get the char code of the previous letter
const prev = array[i - 1].charCodeAt();
// get the char code of the current letter
const current = array[i].charCodeAt();
if (current - prev !== 1) { // if the difference between current and previous is not 1
// get the character after the previous
return String.fromCharCode(prev + 1);
}
}
return null; // if nothing is found
}
console.log(findMissingLetter(['a', 'b', 'c', 'e'])); // d
console.log(findMissingLetter(['l', 'n', 'o', 'p'])); // m
console.log(findMissingLetter(['s', 't', 'u', 'w', 'x'])); // v
console.log(findMissingLetter(['a', 'b', 'c'])); // null
And another solution that uses Array.findIndex()
to find the missing character:
const findMissingLetter = (array) => {
const index = array
.slice(1) // create an array with 1st letter removed
.findIndex((c, i) => c.charCodeAt() - array[i].charCodeAt() > 1); // pare current letter and the same index in the original array 'till you find a missing letter
return index > -1 ? // if we found an index
String.fromCharCode(array[index].charCodeAt() + 1) // if index was found return next letter after the last letter in a sequence
:
null; // if index wasn't found
};
console.log(findMissingLetter(['a', 'b', 'c', 'e'])); // d
console.log(findMissingLetter(['l', 'n', 'o', 'p'])); // m
console.log(findMissingLetter(['s', 't', 'u', 'w', 'x'])); // v
console.log(findMissingLetter(['a', 'b', 'c'])); // null
Using English letters, you can use an array with the letters and check for each letter in the passed array.
let findMissing = (arr) => {
let alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let j = alpha.indexOf(arr[0]);
for (let i = 0; i < arr.length; i++) {
if (arr[i].toUpperCase() !== alpha[j].toUpperCase()) return alpha[j];
j++;
}
return "";
}
console.log(findMissing(['a', 'b', 'c', 'e']));
console.log(findMissing(['l', 'n', 'o', 'p']));
console.log(findMissing(['s', 't', 'u', 'w', 'x']));
console.log(findMissing(['s', 't', 'u', 'v', 'w']));
I came with this solution:
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');
const findMissing = (arr) => {
const start = alphabet.indexOf(arr[0]);
const end = alphabet.indexOf(arr[arr.length-1]);
const sliced = alphabet.slice(start, end + 1);
return sliced.filter((letter) => !arr.includes(letter));
};
console.log(findMissing(['a','b','c','e','h']));
console.log(findMissing(['h','j','l','p']));
console.log(findMissing(['s','t','v','z']));
This will return an array of missing letter(s) between the starting and ending alphabet letter.
You can use charCodeAt
, but on the (single-character) strings in the array:
const findMissingLetter = arr => (cur =>
String.fromCharCode(arr.find(ch => ch.charCodeAt() != cur++)?.charCodeAt() - 1)
)(arr[0].charCodeAt());
console.log(findMissingLetter(['a', 'b', 'c', 'e'])); // 'd'
console.log(findMissingLetter(['l', 'n', 'o', 'p'])); // 'm'
console.log(findMissingLetter(['s', 't', 'u', 'w', 'x'])); // 'v'
console.log(findMissingLetter(['a', 'b', 'c'])); // ''
本文标签: javascriptFind the missing letter in an arrayStack Overflow
版权声明:本文标题:javascript - Find the missing letter in an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745212952a2647996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论