Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.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 创建一个迭代的Javascipt_Javascript_Function_Recursion_Iteration - Fatal编程技术网

Javascript 创建一个迭代的Javascipt

Javascript 创建一个迭代的Javascipt,javascript,function,recursion,iteration,Javascript,Function,Recursion,Iteration,下面我有一个递归函数,我只是想知道如何为同样的事情创建一个迭代(即没有递归的循环)。我真的很感激任何帮助或建议谢谢 function countPalindromes(string, count) { if (string.length <= 1) { return count; } let [ firstLetter ] = string; let lastLetter = string[string.length - 1]; if (firstLette

下面我有一个递归函数,我只是想知道如何为同样的事情创建一个迭代(即没有递归的循环)。我真的很感激任何帮助或建议谢谢

function countPalindromes(string, count) {
  if (string.length <= 1) {
    return count;
  }

  let [ firstLetter ] = string;
  let lastLetter = string[string.length - 1];

  if (firstLetter === lastLetter) {
    let stringWithoutFirstAndLastLetters = string.substring(1, string.length - 1);
    return countPalindromes(stringWithoutFirstAndLastLetters, count + 1);
  } else {
    return count;
  }
}

console.log(countPalindromes("level", 0));
console.log(countPalindromes("aya", 0));
函数countPalindromes(字符串,计数){

如果(string.length您可以执行以下操作:

函数countPalindromes(字符串){
让计数=0;
for(设i=0;iconsole.log(countPalindromes(“aya”);
您可以执行以下操作:

函数countPalindromes(字符串){
让计数=0;
for(设i=0;ilog(countPalindromes(“aya”);
花了我一段时间,但这应该可以用

函数countPalindromes(字符串,计数){
对于(i=0;ilog(countPalindromes(“aya”,0));
花了我一段时间,但这应该可以用

函数countPalindromes(字符串,计数){
对于(i=0;ilog(countPalindromes(“aya”,0));
您可以反转字符串并比较字母,直到遇到不匹配项,返回匹配的最后一个索引加1为止

函数isAlindrome(word){
const drow=word.split(“”).reverse();
对于(i=0;iconsole.log(isAlindrome(“level”);
您可以反转字符串并比较字母,直到遇到不匹配项,返回匹配的最后一个索引加上一个

函数isAlindrome(word){
const drow=word.split(“”).reverse();
对于(i=0;iconsole.log(isAlindrome(“level”);
我没有得到您的递归回文函数。只是将递归函数转换为迭代函数

 function countPalindromes(string, count) {
        if (string.length <= 1) {
            return count;
        }
        let i = 0, j = string.length-1;
        for (; i < j && string[i] == string[j]; i++, j--);
        return i;
    }
    
    console.log(countPalindromes("level", 0));
    console.log(countPalindromes("aya", 0));
函数countPalindromes(字符串,计数){

if(string.length我没有得到你的递归回文函数。只是将递归函数转换为迭代函数

 function countPalindromes(string, count) {
        if (string.length <= 1) {
            return count;
        }
        let i = 0, j = string.length-1;
        for (; i < j && string[i] == string[j]; i++, j--);
        return i;
    }
    
    console.log(countPalindromes("level", 0));
    console.log(countPalindromes("aya", 0));
函数countPalindromes(字符串,计数){

如果(string.length@28space\u junkie是,for
循环使其迭代。@28space\u junkie是,for
循环使其迭代。