My challenge involves working with an array that looks like this:
["Lorem", "Ipsum", "Colo", "sit", "ame", "consecteur"]
The goal is to create subarrays with a combined character length of 10, resulting in something like this:
[
["Lorem", "Ipsum"],
["Colo", "sit", "ame"],
["consecteur"]
]
I attempted the following approach:
var arr = ["Lorem", "Ipsum", "Colo", "sit", "ame", "consecteur"];
var combArr = [];
var charCount = 0;
for (i = 0; i < arr.length; i++) {
charCount += arr[i].length;
if (charCount <= 10) {
combArr.push(arr[i]);
}
if (charCount > 10 && charCount <= 20) {
combArr.push(arr[i]);
}
// ...
}
However, my current method only retains the elements that meet the condition, ultimately maintaining the original order of the items. I am seeking guidance on how to achieve the desired multidimensional array structure as shown above. Any assistance you can provide would be greatly appreciated. Thank you!