JavaScript "firstNonRepeatingLetter"函数返回未定义而不是字符?

标签 javascript arrays string undefined

问题描述如下:

编写一个名为firstNonRepeatingLetter†的函数,该函数接受字符串输入,并返回字符串中任何地方不重复的第一个字符。

例如,如果给定输入“stress”,函数应返回“t”,因为字母 t 在字符串中仅出现一次,并且在字符串中首先出现。

作为一个额外的挑战,大写和小写字母被视为相同的字符,但该函数应返回首字母的正确大小写。例如,输入“sTreSS”应返回“T”。

下面是我的代码,它返回未定义。我无法捕获错误。

function firstNonRepeatingLetter(s) {
  var x = s.replace(" ", "");
  for (var i = 0; i < x.length; i++) {
    if (x.charCodeAt(i) < 96) {
     
      var y = x.replace(x[i], "");
      var z = String.fromCharCode(x.charCodeAt(i) + 32);
      if (y.indexOf(x[0]) > -1 || y.indexOf(z) > -1) {
        continue;
      } else {
        var m = x[i];
      }
      return m;
    } else if (x.charCodeAt(i) > 96) {
      
      var y = x.replace(x[i], "");
      var z = String.fromCharCode(x.charCodeAt(i) - 32);
      if (y.indexOf(x[0]) > -1 || y.indexOf(z) > -1) {
        continue;
      } else {
        var m = x[i];
      }
      return m;
    }
  }
}

最佳答案

您的条件y.indexOf(x[0]) > -1不正确。您不需要比较第个字符,而是需要比较第i个字符。将它们都替换为 y.indexOf(x[i]) > -1

<小时/>

一些提示:

  • 避免使用单字母变量名称。它们使调试和理解代码变得异常困难。
  • block 内的
  • var 声明,如 forif 等,不会做任何有用的事情。您已重新声明 var m 两次。这只会增加困惑。
  • .replace("", "") 仅删除第一个空格,即使您可能打算删除所有空格。使用正则表达式(也可用于添加更多案例,或​​将输入限制为特定字符),或使用 replaceAll (注意浏览器兼容性和提案状态)。使用正则表达式可能类似于 .replace(/\s/g, "") 来删除所有空格,或 .replace(/[^a-z]/gi , "") 删除所有非拉丁字母的内容。
  • 无需从字符码中添加或减去 32,只需使用 .toUpperCase.toLowerCase .
  • 同样,比较小于或大于 96 的字符代码将无法处理具有此精确字符代码的字符,即 ` 字符。相反,请考虑将整个字符串转换为大写或小写,而不是在两种单独的情况下处理这两种情况。
  • 最后,根据分配是否允许,考虑使用 ObjectArray方法。
<小时/>

以下是在 ECMAScript 2015+ 中编写函数的两种方法:

const getFirstUniqueLetter = (string) => {
  string = [...string.replace(/\s/g, "")];

  const {
    map,
    unique
  } = string.reduce((result, character) => {
      result.map[character.toLowerCase()] = (result.map[character.toLowerCase()] || 0) + 1;

      if(!result.unique.includes(character)){
        result.unique.push(character);
      }

      return result;
    }, {
      map: {},
      unique: []
    });

  // You can add the `|| ""` in the next line to get a default output of the empty string, if no character is unique. Otherwise, `undefined` is returned.
  return unique.find((character) => map[character.toLowerCase()] === 1) /* || "" */;
};
const getFirstUniqueLetter = (string) => {
  string = string.replace(/\s/g, "");

  // You can add the `|| ""` in the next line to get a default output of the empty string, if no character is unique. Otherwise, `undefined` is returned.
  return [...string].find((character) => string.match(new RegExp(character, "gi")).length === 1) /* || "" */;
};

关于JavaScript "firstNonRepeatingLetter"函数返回未定义而不是字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51428116/

相关文章:

javascript - 将外部 JS/CSS 添加到 Magento 网站

javascript - 如何按类获取可见元素?

arrays - 如何在Dart中对该数组/映射进行排序?

凯撒密码 (C) : Read from textfile

php - 函数仅返回 mysqli 查询结果的第一个数组值

java - 将字符串拆分为数组并将分隔符添加到数组中

Java从字符串中替换精确的子字符串

javascript - 将存储的 var 传递给 g :remoteLink 的 id 和更新属性

javascript - 如何使用 d3 生成任意数量的颜色?

具有非紧凑字符串的 Java 紧凑字符串 equalsIgnoreCase 计算结果为 false