I'm wrapping my head around the following logic, but I'm still missing something.
Given an Array like const testArr = ["F", "F", "C", "C", "F", "C", "F"]
.
The result array should look like ["F", "F", ["C", "C"], "F", ["C"], "F"]
.
The code I came up until now looks like this:
const grouping = (arr) => {
const result = [];
arr.forEach((item, index) => {
if (item === "C") {
const subArr = new Array();
subArr.push(item);
if (arr[index + 1] !== "C") {
result.push(subArr);
}
} else {
result.push(item);
}
});
return result;
};
console.log(grouping(testArr));
This prints currently the result:
["F", "F", ["C"], "F", ["C"], "F"]
I appreciate your hints :-)