Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在纯函数式JavaScript中:迭代字符串的字符,如果满足条件,则返回一个值_Javascript_Arrays_String_Function_Loops - Fatal编程技术网

在纯函数式JavaScript中:迭代字符串的字符,如果满足条件,则返回一个值

在纯函数式JavaScript中:迭代字符串的字符,如果满足条件,则返回一个值,javascript,arrays,string,function,loops,Javascript,Arrays,String,Function,Loops,我有一个示例函数,其中输入了一系列小写字母,如果这些字母是完全连续的,则该函数返回未定义的;但是,如果丢失的字母破坏了连续性,则会返回丢失的字母 我正在尝试编写更多的功能性/可读性代码。我想知道下面的代码是否可以转换成一种更实用的形式,也就是说,不需要不必要地创建变量 function missingLetterIs(str) { var splittedStr = str.split(''); var answer; var i = 0; var j = 1; while

我有一个示例函数,其中输入了一系列小写字母,如果这些字母是完全连续的,则该函数返回未定义的;但是,如果丢失的字母破坏了连续性,则会返回丢失的字母

我正在尝试编写更多的功能性/可读性代码。我想知道下面的代码是否可以转换成一种更实用的形式,也就是说,不需要不必要地创建变量

function missingLetterIs(str) {
  var splittedStr = str.split('');
  var answer;
  var i = 0;
  var j = 1;
  while (answer === undefined && j < splittedStr.length) {
    var currentLetter = splittedStr[i];
    var nextLetter = splittedStr[j];
    var currentCharCode = currentLetter.charCodeAt(0);
    var nextCharCode = nextLetter.charCodeAt(0);
    if (+nextCharCode - +currentCharCode !== 1) {
      answer = String.fromCharCode(currentCharCode + 1);
    }
    i++;
    j++;
  }
  return answer;
}

console.log(missingLetterIs("abcef"));
// -> d

下面是我将如何简化您的代码。我不认为它比你的功能更差,不过:

function missingLetterIs(str) {
  for (var i = 1; i < str.length; i++) {
    var expected = str.charCodeAt(i-1) + 1;

    if (str.charCodeAt(i) !== expected) {
      return String.fromCharCode(expected);
    }
  }
}
这里有一个方法:

function missingLetterIs(str) {
  function helper(currentIndex) {
    return (
      str.length < currentIndex + 2
        ? undefined
        : str.charCodeAt(currentIndex) + 1 == str.charCodeAt(currentIndex + 1)
           ? missingLetterIs(str, currentIndex + 1)
           : String.fromCharCode(str.charCodeAt(currentIndex) + 1)
    );
  }

  return helper(0);
}

老实说,我不确定这在您的情况下是否有用-长度检查可能会像if块一样清晰,递归实际上可能会像迭代一样清晰-但它向您展示了您可以做什么。

我会获取代码点并检查它们是否连续

function missingLetterIs(str) {
  var prev = -1;
  for(var character of str) {
    var curr = character.codePointAt(0);
    if(prev >= 0 && curr - prev !== 1)
      return String.fromCodePoint(prev+1);
    prev = curr;
  }
}

以下是一个简单的功能方法:

"abcef"
 .split("")
 .map(a=>a.charCodeAt())
 .filter((a,b,c)=>c[b+1] && a+1!=c[b+1])
 .map(a=>String.fromCharCode(a+1))[0];
如果集合如预期的那样,它将生成缺少的字母或未定义的字母

对我来说,函数的优点至少是能够将一个复杂的问题分解为一系列离散的步骤

如果我以后需要重用这些步骤中的任何一个,我可以给该步骤的anon函数起一个名称,然后从其他地方调用它,而不需要重复,而您不能循环使用for循环的中间部分

示例:步骤.filtera,b,c=>c[b+1]&&a+1=c[b+1]可以成为

.filter(isBigger)
通过定义命名函数:

var isBigger = (a,b,c)=>c[b+1] && a+1!=c[b+1];

这是一个纯功能解决方案:

//让我们定义一个适用于数字序列的通用函数: checkTerm=seq,term=> !长度?无效的: seq[0]==术语?缺失术语Seq: 学期 missingTerm=seq=>checkTermseq.slice1,seq[0]+1 //这两个不是很可爱吗? console.logmissingTerm[1,2,3,4]//null console.logmissingTerm[1,2,4,5]//3 //然后将其应用于特定的大小写字符串 fromCharCode=x=>x&&String.fromCharCodex missingChar=str=>fromCharCode missingTerm[…str].mapx=>x.charCodeAt0 console.logmissingChar'bcdef'//null
logmissingChar'bcdfg'/'e'在性能方面,对于长字符串更好的方法是使用递归函数进行二分法搜索

函数missingLetterIsstr{ 返回recursiveSearchstr,0,str.length-1; } 函数recursiveSearchstr,start,end{ var sz=结束-开始; 回来 str.charCodeAtend-str.charCodeAtstart==sz?错误: sz==1?String.fromCharCodestr.charCodeAtstart+1: 递归搜索str,start,start+sz>>1|| 递归搜索str,开始+sz>>1,结束 ; }
console.logmissingLetterIsabcdefghijklmnopqrstvwxyz 也许你可以这样做

函数getMissingChars{ var mi=Array.prototype.findIndex.calls,e,i,a=>iconsole.loggetMissingCharabce;另外,nextCharCode和currentCharCode已经是数字了,因此您不需要在if语句中将它们转换为数字。到目前为止,这是可读性和功能性的最佳平衡。