Right now I am trying to create an array of objects that have numbers greater than 0, and when the number is 0 the array ends , when the number is greater than 0 a new array should be created (therefore the function should return an array of arrays, broken up when there are 0's in the middle.
Input and desired output:
input = [1,2,3,4,0,0,0,9,9,0,0]
output = [[1,2,3,4][9,9]]
My function (so far):
function group(Array) {
let allArray = []
let runGroup = new Array()
Array.forEach(function(runtimeI,index) {
if (runtimeI > 0)
{
runGroup[i] = new Array()
runGroup[i].push(runtimeI)
}
}
)
allArray.push(runGroup[i])
}
array = [10,0,0,1,1,2,0,0,15,0,0,0,5,5,5,]
console.log(group(array))
The function I created throws an error saying array is not a constructor
working example function (that creates the kind of structure I need)
var squares = new Array();
for(var i = 0; i <= 8; i++)
{
squares[i] = new Array();
for(var j = (i * 20) + 1; j <= 20 * i + 20; j++)
if (squares[i] == null)
squares[i] = j;
else
squares[i].push(j);
}
console.log(squares)
so I guess my question is, is there something about for loops that forEach can't do? is there another way I should be accomplishing this?

.forEach()code you posted has several errors; have you checked the console?Array.forEach? you're just calling a forEach on...nothing in particular. What should this iterate over?group(Array)that's worse!new Array()now does NOT make a new array but callsnewon the input.Arrayconstructor function for a start. It can also help to use the array literal initialiser -[]instead ofnew Arraybut shadowingArrayis still confusing.function group(Array) {— Don't mask global functions with local variables that have the same name. Don't use variables that start with a capital letter for things that are not constructor functions.