0

I am trying to split a string and combine it based on its index. A trivial example will be:

Input: "ABCABCABC" Output: "AAABBBCCC"

So I created a function that converts the string into an array pops off the elements into its appropriate array and hopefully combine it back to a string.

Here's the function:

function splitter(string, numSplit) {
  let splitHolder = [];
  let splitArr = string.split("");
  while (splitArr.length > 0) {
    for (let i = 0; i < numSplit; i++) {
      splitHolder[i] = splitHolder[i] === undefined
          ? [splitArr.shift()]
          : splitHolder[i].push(splitArr.shift());
      console.log(splitHolder);
    }
  }
}

Results for the console.log shows:

[ [ 'A' ] ]
[ [ 'A' ], [ 'B' ] ]
[ [ 'A' ], [ 'B' ], [ 'C' ] ]

which indicates the function is running okay at least on the first loop. However on the second pass of the for loop after it is checked by the while. I am getting this log:

[ 2, [ 'B' ], [ 'C' ] ]
[ 2, 2, [ 'C' ] ]
[ 2, 2, 2 ]

Which is very strange as I am still trying to push what is remaining of the array? Where did the numeric 2 come from?

Initially thought it was a scoping issue but it wasn't as the let is scoped for the whole function.

Also tried the array reduce function but am still getting the same numeric 2.

3
  • 1
    'ABCABCABC'.split('').sort().join('') Commented Jul 14, 2020 at 14:27
  • Array#push returns the new length of the array, and you're doing splitHolder[i] = splitHolder[i].push(...); Just use an if statement like everybody else. Commented Jul 14, 2020 at 14:27
  • Thanks for the comment of @NiettheDarkAbsol pointing me to the right direction on the what is causing the appearance of the number 2. It is indeed the length of the array. Commented Jul 14, 2020 at 14:39

2 Answers 2

1

You could take Array#concat instead of Array#push.

concat returns a new array and push the new length of the array.

function splitter(string, numSplit) {
    let splitHolder = [];
    let splitArr = string.split("");
    while (splitArr.length) {
        for (let i = 0; i < numSplit; i++) {
            splitHolder[i] = splitHolder[i] === undefined
                ? [splitArr.shift()]
                : splitHolder[i].concat(splitArr.shift());
        }
    }
    return splitHolder;
}


console.log(splitter('ABCABCABC', 3));

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Nina. Really missed that push return value. Leaned something new today.
0

Array.push() return new length of the array after push

 let arr =['A', 'B']

when you will push into this array.

arr.push('C') // return 3 . i.e. length of the array.

I think you don't want to push into splitHolder but concatenate the first element of splitArr .

function splitter(string, numSplit) {
  let splitHolder = [];
  let splitArr = string.split("");

  while (!!splitArr.length) {
    for (let i = 0; i < numSplit; i++) {
      splitHolder[i] = (splitHolder[i] === undefined)
          ? [splitArr.shift()]
          : splitHolder[i].concat(splitArr.shift());
      console.log(splitHolder);
    }
  }
 return splitHolder;
}

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.