Chunky Monkey Assistance

I know there a million ways to do this, but I want to figure out this way since it’s the way I started and I’m invested now.

I’ve actually spend a few hours on this now and I’m not sure if this is the only problem, but it’s a major one-

The `else if`` is not executing.

function chunk(arr, step){

 var holder_arr = []

 if (arr.length > step) {

for(var i=0;i<arr.length;i++){
  console.log(true)


  var spliced = arr.splice(0,step) //splice off arr and

  holder_arr.push(spliced) //push slices

  }
} else if (arr.length < step) {
  console.log(false)
  holder_array.push(arr)


}
   return holder_arr
}

chunk([0, 1, 2, 3, 4, 5, 6],3)

At the end only 6 is left. And this is an array length of one, but the function does not go inside the else if to complete the statement. Can someone tell me why? I tested it with just true/false and it worked so it’s the code at hand causing the problem. I also tried moving the loop around and this didn’t help.
As well I tried i >= & i > the middle section of the for loop, just to see if I had this wrong. Made no difference.

The key word is else. If you use else only one of the if statements will be executed.

Man, this is weird. It always happens that I click space bar and it posts the post before I am ready.

It’s that leftover part I don’t know how to get. I don’t know how to get that.

This is the code again the some comments used to debug. Using else or else if makes no differnce I tried both before I posted .

function chunk(arr, step){

 var holder_arr = []

if (arr.length > step) {

for(var i=0;i<arr.length;i++){
  console.log(true)

  console.log(arr + " arr at top of loop")

  var spliced = arr.splice(0,step) //splice off arr and
  console.log(spliced + " spliced")

  console.log(arr + " arr after splice")

  holder_arr.push(spliced) //push slices
  console.log("spliced pushed")
  console.log(arr.length)
  }
} else {
  console.log(false)
  holder_array.push(arr)
  console.log("waka waka");

 }
  return holder_arr
 }

chunk([0, 1, 2, 3, 4, 5, 6],3)

After I run it I get
true 0,1,2,3,4,5,6 arr at top of loop 0,1,2 spliced 3,4,5,6 arr after splice spliced pushed 4 true 3,4,5,6 arr at top of loop 3,4,5 spliced 6 arr after splice spliced pushed 1

So it shows that arr.length is 1. Why doesn’t it run?

But did you try to use if instead of else if?

You mean like

 function chunk(arr, step){
   var holder_arr = []

    if (arr.length > step) {

   for(var i=0;i<arr.length;i++){
      [....]
    } else {
     [...]

    }
   holder_arr
  }
chunk([0, 1, 2, 3, 4, 5, 6],3)

Like replace the previous else if with else? If I run the same code with else it does not run the else block. At least in my dev env.

Not sure if that’s wrong I get two arrays, with the number 6 being left off.
[ [ 0, 1, 2 ], [ 3, 4, 5 ] ]

What kind of operation allows me to move on to the next block?

Read my post again.``

So just use a plain if?

Function
 if
 [...]
  if
 [...]
end

This give me back [ [ 0, 1, 2 ], [ 3, 4, 5 ] ]

I’ve tried now if/if , if/else if, if/else. All give the same return value. Perhaps you could give an example? There must be more to change that I’m missing

I tried it using a for loop and no conditionals but it left off that last odd element everytime. Thus I had the even numbered arrays correct, and the odd incorrect. That was when I switched to conditionals.

On the other hand, the double if does work for an odd number of items. I didn’t know you could use if in this way. I thought if had to be followed by an else of some type.

However, for even numbered arrays it screws up the answer. I guess I need another conditional? This would essentially double the code’s size though.

As for writing out the step as you say, that was done long before I came here. I tried everything first before asking for help. I wanted to see my approach through, and not change it. Obviously there are lots of other discussions on this exact problem I could have used, but I wanted to see if I could do it alone. That didn’t happen

So I think this has turned into a monster and it might be time to kill it.

Thanks for taking the time out to help!!! I’ll go over to one of those other discussions and check out the proper way to do it

Using the outline that you provided, and after learning how to use slice and splice , this is my answer. It passes.

function chunkArrayInGroups(arr, size) {
var holder_arr = [];
 for (var i = 0; i <= arr.length; i++) {
  var spliced = arr.splice(0,size);
  holder_arr.push(spliced);
 }
if ((arr.length < size) && (arr.length > 0)){
holder_arr.push(arr)
 }
return holder_arr;
}

I believe I only added in an iteration as you say, at first, but a for loop. It didn’t work so I had to add the last line.
But I guess that’s your point. That the while would have been better.

Unfortunately I’m nowhere near the point where I am worrying about efficiency. I wish I was but I am not. Just getting things to work is all I can manage ATM.

But thanks for the heads up with splice. It is something for me to keep in mind when I’m going to use it later. When possible I will use slice.

I’m happy to have any more advice for future problems, as you’ve given me already.