Chunky Monkey - Creating MultiDimensional Arrays?

Tell us what’s happening:
My commented out comments I think show that I am quite unsure of the appropriate method to tackle this final basic algorithm challenge. Funny enough, I am getting the appropriate printout in the console…It’s just that It’s not within another array. I’m assuming that I have to create another for loop to make the 2nd dimensional array? This quite a difficult end challenge.

Your code so far


/*Basic Algorithm Scripting: Chunky Monkey
Write a function that splits an array (first argument)
into groups the length of size (second argument) and returns
 them as a two-dimensional array.*/

 /*take the first group of information in the array and use the split() method
 into different groups which will have to be determined by the 'size'
 parameter */ 

function chunkArrayInGroups(arr, size) {
    let newArr = []
      outter: for(let i = 0; i < arr.length; i ++){
       //  inner: for(let j = 0; j < arr.length; j++){
       //       console.log(arr[j]);
           let firstArr = (arr.slice(0,size));
               newArr.push(firstArr);
             let secondArr = (arr.slice(size));
               newArr.push(secondArr);
        }
 return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Chunky Monkey

Link to the challenge:

Yeah, you are off to a good start.

for( ; ; ){
  for( ; ; ){
  }
}

Now let’s figure out what goes in the parentheses ?
Got any ideas ?

Here is a hint…
the inner loop must run “size” times in order
to make a “Chunk” of “size” length

the outer loop must run the inner loop
enough times to consume the array

if array length is say 13
and the size of each chunk is 4
then one array[item] will not be consumed
so the last chunk will have a length of one

yes it is Because it is very very simple
you are OverThinking this problem

1 Like

hmmm. I think you may be right that I’m over thinking it.

     let newArr = []
       outter: for(let i = 0; i < arr.length; i ++){
              console.log(arr[i])
         inner: for(let j = 0; j < size; j++){
              console.log(arr[j]);
                newArr.push(arr.slice(size));
           }
        }
  return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));```

I think looping through the array and the size argument would make sense, I just don't understand in a way to slice it appropriately as such. But I will try to think of a more simpler solution.

no slicing is required

Here is a hint…
the inner loop must run “size” times in order
to make a “Chunk” of length = “size”

the “slicing” is accomplished by
the inner and outer loops
the inner loop creates the “chunks”

1 Like

Sorry just got home from work. I wish I had more time to program!!! I can’t wait to change careers. I’m not quite sure I understand what you’re saying. It sounds like I am running the outer loop through the original array and then the inner loop through the length of the size argument. But that can’t be right as I am getting undefined.

function chunkArrayInGroups(arr, size) {
     let newArr = []
       outer: for(let i = 0; i < arr.length; i ++){
              console.log(arr[i])
               
         inner: for(let j = 0; j < size; j++){
              console.log(arr[j][i]);
              newArr.push(arr[j][i])
           }
        }
  return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));

well I’m happy to see you simplified it
maybe I wasn’t clear about consuming the array

the outer loop must run the inner loop
enough times to consume the array

so the inner loop consumes size number
of array items each time it runs

yo are running the outer loop array.length times
outer loop should run only enough times
so that size consumes all the items

the inner loop consumes size number at a time
so how many times do you have to subtract size
fom array.length to consume all the items ?


I’m sorry that’s insanely confusing. I don’t think I know nested for loops that well enough to grasp what you’re saying. The outer loop isn’t the array length? Then is must be just the array itself:

for(i =0 ; i < arr; i++)
and the inner array isn’t running through the length of the size?
am I incrimenting it by the size?
for(let i = 0; i < arr[i]; size)???

Totally lost

var totalLength=12;

for(let i=0 ;i<3 ; i++){

  for(let i= 0;i<4 ; i++){

    totalLength=totalLength - 1

      console.log( totalLength)    
  }
}

logs
11
10
9
8
7
6
5
4
3
2
1
0


    let newArr = []

      outer: for(let i = 0; i < size; i++){

             console.log(arr[i]);

        inner: for(let j = 0; j < arr; j++){

             console.log(arr[j]);

            

          }

       }

 return newArr;

}

console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));

well i don’t know about inner for loop, but i got a and b alone on outter loop

lookin good
nothing needs to happen in i loop
all the pushing is in the j loop

But

I still have not made it clear that the product
of the outer loop and the inner loop will be
an integer that is equal to or less than
array.length

if array length is say 13
and the size of each chunk is 4
then one array[item] will not be consumed

if the product is less than array length
then some items will be left over so the last chunk
will be less than size

so the inner loop is size
what is the outer loop
it is array.length divided by size

So wait…

/*Basic Algorithm Scripting: Chunky Monkey
Write a function that splits an array (first argument)
 into groups the length of size (second argument) and returns
  them as a two-dimensional array.*/

  /*take the first group of information in the array and use the split() method
  into different groups which will have to be determined by the 'size'
  parameter */ 

  function chunkArrayInGroups(arr, size) {
    let newArr = []
      outer: for(let i = 0; i < size; i++){
             console.log(arr[i]);
        inner: for(let j = 0; j < arr[i].length; j++){
             newArr.push(arr[i][j]);
          }
       }
 return newArr;
}
console.log(chunkArrayInGroups(['a', 'b', 'c', 'd'], 2));
console.log(chunkArrayInGroups([0,1,2,3,4,5], 4));

Am i incorrect in having size in the outer loop?

Yes you are incorrect
the outer loop is
for(i =0 ; i < arr.length/size; i++)

newArr.push(arr[i][j]);
this is also incorrect
arr[i][j] would dereference a two dimensional array
you are working with a one dimensional array

I’m not sure where the the double loops came in, but I suspect this is becoming more complex than you want @CodingDutchman.

How would you do this task by hand? I give you a list of 20 numbers and ask you to copy the list onto a fresh sheet of paper, but put three numbers on each line of your new sheet of paper instead of one. What steps, exactly, do you take to accomplish this task?

1 Like

I would not be able to do 3 numbers for each line, but I would write 3 numbers on 6 lines and the 7th line would have 2 numbers.

2 Likes

OK, so how how can we start making that description sound like a loop? What is the task that is repeated? How do you know when you have done it enough times?

1 Like

The condition part of the for loop would be the task that is repeated until that condition is met. I don’t understand how to apply that to the parameter array though? I guess that’s where I am getting lost.

the idea is to make
(array.length divided by size) number of arrays
and push them into a container array
and one more array if needed to hold any left overs ,
also pushed into the container array

the arrays are populated with size number
of items if any items are left they populate
one more array which is pushed into the
container

function chunkArrayInGroups ( arr, size ) {
     //outer loop
pseudo	make an array named tempArray;
pseudo	make tempArray equal an empty array;
pseudo	make an array named finalArray;
pseudo	make finalArray equal an empty array;
pseudo	make a variable named length;
pseudo	set length to the integer value of arr.length / size;
pseudo	make outerLoop run length number of times{
		// the inner loop
	pseudo	make innerLoop run size number of times{ 
		// here we copy out `size` items at a time by
		// pushing arr[items] into tempArray
		// we will not use arr[index]
		// instead we will shift off the items
		// https://freecodecamp.github.io/wiki/en/js-array-prototype-shift
		pseudo	push the shifted values into tempArray;	
		};
		// end of inner loop
		//the tempArray will be pushed into finalArray
		pseudo	push tempArray into finalArray;
		// then tempArray is re initalized
		pseudo	make tempArray equal an empty array;
     	}//end of outer loop
	pseudo chech if any values are left in arr{
	pseudo	push arr into finalArray
	};
	return finalArray
}

lacking any complexity

You said the task is to copy size (3) numbers per line. How do you know to stop copying numbers from the list I gave you?

Edit: I’m off to bed, but I’d recommend you try to look at how you know which size numbers to copy onto each line (or into each new ‘chunk’ array). You’re going to need to loop through your entire list, copying out size items at a time.

the outer loop is
for(i =0 ; i < arr.length/size; i++)

the inner loop is
for(i =0 ; i < size; i++)

the total loop count will be the product of
the outer count and the inner count
arr.length/size times size equals arr.length