# Chunky Monkey Assistance

Chunky Monkey Assistance
0

#1

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.

#2

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

#3

The reason you will not enter the else if is because your if…else if statement is based on arr.length at the very start (so unless arr has less elements that step at the very beginning, your code will never execute any code in the else if portion.

For example, if you make the following statement and change your holder_array.push(arr) line to holder_arr.push(), you will get into the else because the starting length of arr is 2 and step is 3.

``````chunk([0, 1],3); // results in [0,1]
``````

With this knowledge, see if you can figure out where to modify your code to get the “leftover” part of the arr.

#4

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?

#5

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

#6

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?

#7

Read my post again.``

#8

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

#9

You should get rid of the if and the else if. It is not necessary. There is a secondary issue in your for statement. You will exit your for loop when i >= arr.length which is going to cause you trouble on one of the tests.

Instead of tweaking your code over and over, you really need to write out the algorithm or steps in plain English or whatever language you want which will get a solution.

The algorithm is basically the following:

1. Start at first array element (index 0)
2. create chunk (you can use your splice statement)
3. push chunk into new array
4. repeat steps 2 & 3 until arr length is zero
5. return new array

Notice there are no if conditions in my algorithm

#10

I can rewrite my basic algorithm another way below which may give you a hint of the JavaScript statements you need:

1. Start at first array element (index 0)
2. While arr length is not zero repeat steps 3 and 4 otherwise skip to step 5
3. create chunk (you can use your splice statement)
4. push chunk into new array
5. return new array

#11

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

#12

I have taken the last algorithm I gave you and using code you already created, I then created the following code with one line of code (2nd of function) for you to fill in. I took out all of the unnecessary steps.

Whenever I get into a situation where I am struggling with the code for my algorithm, I will step back and rewrite the algorithm from scratch and run through test examples on paper to make sure my written algorithm will solve all of them.

``````function chunk(arr, step) {
var holder_arr = [];
// implment some type of iteration here
var spliced = arr.splice(0,step); //splice off arr and
holder_arr.push(spliced); //push slices
} // close of iteration
return holder_arr;
}
``````

#13

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;
}``````

#14

That works, but I will share the solution I was trying to get you to use. My post before this one, I had given you:

``````function chunk(arr, step) {
var holder_arr = [];
// implment some type of iteration here
var spliced = arr.splice(0,step); //splice off arr and
holder_arr.push(spliced); //push slices
} // close of iteration
return holder_arr;
}
``````

All you needed to do was change line #3 (see below):

``````function chunk(arr, step) {
var holder_arr = [];
while (arr.length) {
var spliced = arr.splice(0,step); //splice off arr and
holder_arr.push(spliced); //push slices
} // close of iteration
return holder_arr;
}
``````

Since you were splicing each chunk, arr was getting smaller each time. My while statement tests where or not the arr length is greater than 0. If the number of items remaining is less than step, the splice will still attempt to capture chunks less than step size.

I personally do not advise using splice, because it creates a lot of extra work “behind the scenes”, because the array as to “readjust” after every splice. Algorithms using Slice would be better because for one, the original array would not be affected and second, there would not be any extra “behind the scenes” juggling of the array. I did not mention this in the beginning, because I wanted you to figure out the solution based on how you had already approached the problem.

#15

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.