0

我正在尝试创建一个函数,该函数接受字母表中连续字符的数组输入,如果有一个则返回丢失的字母(只有 1 个丢失的字母,并且数组中的每个元素都将按字母顺序列出)。

示例输入:

['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));

我正在尝试做的是遍历数组的每个索引并将每个字符转换为 unicode。如果数组中的 [i + 1] - [i] 元素等于 1,则没有缺少字母。但是,如果它不等于 1,那么我想返回 [i] + 1 的 unicode,然后通过高阶函数将 unicode 输出转换回字母表中的相应字符。

有人可以解释我做错了什么吗?我知道我没有正确调用函数。

谢谢!

4

4 回答 4

3

string 方法.charCodeAt()不适用于数组。您需要在每个字符上使用它,并在位置 0(默认值)处获取代码:

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

还有另一个Array.findIndex()用于查找丢失字符的解决方案:

const findMissingLetter = (array) => {
  const index = array
    .slice(1) // create an array with 1st letter removed
    .findIndex((c, i) => c.charCodeAt() - array[i].charCodeAt() > 1); // compare 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

于 2018-11-04T19:46:31.280 回答
0

我提出了这个解决方案:

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']));

这将返回开始和结束字母之间的缺失字母数组。

于 2018-11-04T20:24:18.747 回答
0

您可以使用, 但在数组中charCodeAt的(单字符)字符串上:

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'])); // ''

于 2021-09-20T20:23:43.477 回答
0

使用英文字母,您可以使用带有字母的数组并检查传递的数组中的每个字母。

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']));

于 2018-11-04T19:48:35.573 回答