1

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.

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

2
  • So, why did you delete the other question? Commented Sep 19, 2016 at 0:37
  • I'll undelete it shortly. I'm just waiting until I can accept. Commented Sep 19, 2016 at 0:40

2 Answers 2

2

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]));
Sign up to request clarification or add additional context in comments.

1 Comment

Yep, a simple return. Thanks @MaxLeizerovich :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.