简体   繁体   English

递归函数返回空数组

[英]Recursive function returns empty array

I have created the following function that aims to split a string to the points requested and returning an array of the words that occurred. 我创建了以下函数,旨在将字符串拆分为请求的点并返回发生的单词数组。

Currently, the function works as it has to, when the arguments are passed one after another, but not when passed inside an array and I want to cover both options. 目前,当参数一个接一个地传递时,该函数可以正常工作,但是当在数组中传递时,我想要覆盖这两个选项。

I have attempted to force the function to rerun itself, if the argument is found to be an array (line 15) , but after it runs for a second time the returned array is empty. 我试图强制该函数重新运行,如果发现该参数是一个数组(第15行) ,但在它运行第二次之后,返回的数组为空。

Question: 题:

How can fix my code so that the function works properly even when the arguments are passed inside an array? 如何修复我的代码,以便即使在数组中传递参数时函数也能正常工作?

Code: 码:

 function string(str) { /* Throw error if 'string' is not a string */ if (str.constructor !== String) throw new Error("Argument is not a string."); else return { splitAt: function() { var args = arguments, len = args.length, arg = args[0], index = 0, prev = 0, arr = []; /* If the indices are passed in an array ([4, 5, 8, 12]) rerun the function */ if (args[0].constructor === Array) string(str).splitAt.apply(this, args[0]); else for (; index < len; index++, arg = args[index], prev = args[index - 1]) { /* The first time cut the string at the requested point and save both parts */ if (!index) { arr[index] = str.substr(0, arg); arr[index + 1] = str.substr(arg); /* From then on overwrite the last element of the array */ } else { arr[index + 1] = arr[index].substr(arg - prev); arr[index] = arr[index].substr(0, arg - prev); } } return arr; } } } /* Arguments passed one after another */ console.log(string("Whatadayit'sbeen!").splitAt(4, 5, 8, 12)); /* Arguments passed in an array */ console.log(string("Whatadayit'sbeen!").splitAt([4, 5, 8, 12])); 

I think you missed a return here: 我想你错过了回归:

function string(str) {
  /* Throw error if 'string' is not a string */
  if (str.constructor !== String) throw new Error("Argument is not a string.");
  else return {
    splitAt: function() {
      var
        args = arguments,
        len = args.length,
        arg = args[0],
        index = 0,
        prev = 0,
        arr = [];

      /* If the indices are passed in an array ([4, 5, 8, 12]) rerun the function */
      if (args[0].constructor === Array) 
        return string(str).splitAt.apply(this, args[0]);
      else for (; index < len; index++, arg = args[index], prev = args[index - 1]) {
        /* The first time cut the string at the requested point and save both parts */
        if (!index) {
          arr[index] = str.substr(0, arg);
          arr[index + 1] = str.substr(arg);

        /* From then on overwrite the last element of the array */
        } else {
          arr[index + 1] = arr[index].substr(arg - prev);
          arr[index] = arr[index].substr(0, arg - prev);
        }
      }
      return arr;
    }
  }
}

/* Arguments passed one after another */
console.log(string("Whatadayit'sbeen!").splitAt(4, 5, 8, 12));
debugger;
/* Arguments passed in an array */
console.log(string("Whatadayit'sbeen!").splitAt([4, 5, 8, 12]));

I've left all your code in tact but modified the part accepting an array argument. 我已经完成了所有代码,但修改了接受数组参数的部分。

 function string(str) { /* Throw error if 'string' is not a string */ if (str.constructor !== String) throw new Error("Argument is not a string."); else return { splitAt: function() { var args = arguments, len = args.length, arg = args[0], index = 0, prev = 0, arr = []; /* If the indices are passed in an array ([4, 5, 8, 12]) rerun the function */ if (args[0].constructor === Array) { return string(str).splitAt.apply(this, args[0]); } else for (; index < len; index++, arg = args[index], prev = args[index - 1]) { /* The first time cut the string at the requested point and save both parts */ if (!index) { arr[index] = str.substr(0, arg); arr[index + 1] = str.substr(arg); /* From then on overwrite the last element of the array */ } else { arr[index + 1] = arr[index].substr(arg - prev); arr[index] = arr[index].substr(0, arg - prev); } } return arr; } } } /* Arguments passed one after another */ console.log(string("Whatadayit'sbeen!").splitAt(4, 5, 8, 12)); /* Arguments passed in an array */ console.log(string("Whatadayit'sbeen!").splitAt([4, 5, 8, 12])); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM