2016-09-18 11 views
1

文字列を要求された点に分割し、発生した単語の配列を返すことを目的とした次の関数を作成しました。再帰関数は空の配列を返します

現在、関数は引数が次々に渡されるときには機能しますが、配列内を渡されるときには機能しません。両方のオプションをカバーしたいと思います。

引数が配列(15行目)である場合、関数を強制的に再実行しようとしましたが、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) 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

うん、単純な** 'return' **。ありがとう@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]));

関連する問題