2016-09-18 145 views
1

我已经创建了以下函数,旨在将字符串拆分为请求的点并返回发生的单词数组。递归函数返回空数组

目前,该函数的工作原理是,当参数一个接一个地传递,但不传递给一个数组时,我想覆盖这两个选项。

我试图强制函数重新运行自己,如果参数被发现是一个数组(第15行),但第二次运行后返回的数组是空的。

问:

如何解决我的代码,这样的功能的正常使用,即使参数是一个数组内传递?

代码:

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]));

+0

那么,你为什么要删除其他问题吗? – zerkms

+0

我很快就会取消删除。我只是等到我可以接受。 –

回答

2

我想你错过了一回这里:

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])); 
+0

是的,一个简单的**'返回**。谢谢@MaxLeizerovich :) –

2

我已经离开所有代码机智,但将部分接受数组参数。

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]));