在数组中找到丢失的字母

时间:2018-11-04 19:35:02

标签: javascript arrays fromcharcode

我正在尝试创建一个函数,该函数接受字母中连续字符的数组输入,如果有一个则返回缺失的字母(只有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输出转换回字母中的相应字符。

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

谢谢!

3 个答案:

答案 0 :(得分:1)

字符串方法.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

答案 1 :(得分: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']));

答案 2 :(得分: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']));

这将返回在开头和结尾字母之间的缺少字母的数组。