Tell us what’s happening:
I know the problem is that the Math.ceil method is returning the wrong number in the for loop’s condition but I don’t understand why that is. If I assign the condition to variable like x and use that variable as the condition, it works perfectly:
let x = Math.ceil(arr.length / size);
for (let i = 1; i <= x; i++) {
//push array of required size into returning array
newArr.push(arr.splice(0, size));
}
How do I implement the Math.ceil method straight into the for loop’s condition without having to use a variable as a placeholder?
Your code so far
function chunkArrayInGroups(arr, size) {
let newArr = [];
for (let i = 1; i <= Math.ceil(arr.length / size); i++) {
//push array of required size into returning array
newArr.push(arr.splice(0, size));
}
console.log(newArr);
return newArr;
}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/chunky-monkey