2016-09-18 4 views
1

Ich habe die folgende Funktion erstellt, die eine Zeichenfolge auf die angeforderten Punkte aufteilen und ein Array der aufgetretenen Wörter zurückgeben soll.Rekursive Funktion gibt leeres Array zurück

Momentan funktioniert die Funktion wie es sein muss, wenn die Argumente nacheinander übergeben werden, aber nicht, wenn sie innerhalb eines Arrays übergeben werden und ich beide Optionen abdecken möchte.

I haben versucht, die Funktion zu zwingen, sich erneut auszuführen, wenn das Argument gefunden wird ein Array (Zeile 15), sondern geben, nachdem er zum zweiten Mal läuft, ist das zurückgegebene Array leer.

Frage:

Wie kann meinen Code beheben, so dass die Funktion richtig funktioniert sogar, wenn die Argumente innerhalb eines Arrays übergeben werden?

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

+0

Also, warum hast du die andere Frage löschen? – zerkms

+0

Ich werde es in Kürze wiederherstellen. Ich warte nur, bis ich akzeptieren kann. –

Antwort

2

Ich glaube, Sie hier eine Rückkehr verpasst:

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

Ja, eine einfache ** 'Rückkehr' **. Danke @MaxLeiserovich :) –

2

verließ ich habe den gesamten Code in Takt aber modifiziert, um das Teil ein Array Argument zu akzeptieren.

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

Verwandte Themen