Tell us what’s happening:
In console, everything logs perfectly fine but the test cases are failing. The only difference I can see is that my array have spaces is between.
**Your code so far**
function chunkArrayInGroups(arr, size) {
let arr2 = [];
for(let i = 0; i < arr.length; i++){
arr2.push(arr.slice(0, size));
arr.splice(0, size);
}
if(arr.length){
arr2.push(arr);
}
}
chunkArrayInGroups(["a", "b", "c", "d"], 2)
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36
So, I added this to add the leftover element at the end of ‘arr2’.
For example in conditions like this - chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) where there is a leftover element 8 needs to be added at the end.
After reading it again - it does make sense. The reason is that any value other than 0 is “truthy” → meaning it will get evaluated as true.
Hence the condition is checking if arr has any elements left which have to be pushed into arr2.
In other words if(arr.length) is identical to if(arr.length!=0)
I appreciate you looking into my code.
May be this is not the correct way to achieve the solution but if you’ll log the function in console you’ll get the correct output, be it any argument.
I am still curious to know why the test cases are being failed when the logged output in the console is correct.
And if the reason is spaces in the logged arrays, then how those spaces are being added when there is no space there in the given argument.
When I copy-paste your code, add the return arr2 → then it’s still failing the last test. Reason is because the for-loop is not appropriate for this challenge and passes the previous tests more as an accident.
Actually, I think this way may be possible. If you add a condition in the for loop to check if arr is bigger than size, and bring the last if condition outside the loop. This will work, seeing that arr is gradually becoming smaller.
Here Array.prototype.push() - JavaScript | MDN it says that push can be used with an array.
Or am I missing something?
EDIT: of course arr. length in the for loop should be devided by size.
It is, although adding just a condition without meaningful counter to the for-loop is bad practice as that’s not the purpose of a for-loop. Ofcourse calculating the number of loops beforehand by dividing the length by the size and then making a for-loop is a valid strategy.
Guess saying a for-loop would not be appropriate for this is not entirely correct. It can work with some additional preparations. I was just thinking about while-loops as the more suited approach, but it’s not the only option.
I was keeping in mind that at this place in the curriculum functional programming is not yet covered. With respect to not mutating anything unnecessarily.
Sure, but you don’t need to know functional programming to know that if you have a loop with the exit condition i < arr.length but you keep changing arr.length, then your logic will be a massive, messy headache.