function chunkArrayInGroups(arr, size) {
var result = [];
var sliced = arr.slice(size);
for (let i = 0; i < arr.length; i++) {
if (arr[i] < size) {
result.push(arr[i]);
}
}
result.push(sliced);
return result;
}
console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36.
I suggest you console what you are returning and invoke the function with the parameters provided in the test description. You will see where you are with this.
You can use slice to answer the question quite easily. Look at the hint below.
function chunkArrayInGroups(arr, size) {
var result = [];
for (let i = 0; i < arr.length; i += size) {
// Slice here and push in result.
}
return result;
}
EDIT.
What you need to figure out is slice from what index to what index.
Not really. chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) should be returning [[0, 1], [2, 3], [4, 5]]. The clue here is in “chunking” (grouping) elements in the provided array into arrays of length equal to second argument.
Hi, I’m not sure what to do. I’m here expecting that the for loop increases by size and slices in this case with index 0,2 [0,1] → 2, 4 [2,3] → 4,6 [4,5], then pushing arr into result array and returning result.
function chunkArrayInGroups(arr, size) {
var result = [];
for (let i = 0; i < arr.length; i += size) {
arr.slice([i], size);
result.push(arr);
}
return result;
}
console.log(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2));
As far as I’m aware, all of these challenges can be solved with if, for and while. It is perfectly fine to restrict yourself to syntax you are comfortable with and then refactor to more idomatic syntax once your solution works.
Don’t get me wrong but it seems a bit like when you see a challenge, you immediately start hacking away with only a faint plan of how it “should probably work”. If you get it done, it’s fine (pure luck though, not skill), and if you don’t, you look at the solution.
Not trying to put you in a corner but that’s sort of the vibe I’m getting.
As for this challenge, your general concept is perfectly fine. You have a result array, and then you go through a loop and push certain parts of the original array in there. You just haven’t thought it through.
Take a pen and paper and write down the indices that the slice method needs in each step. It takes numbers, nothing else.
step 1: from 0 to 2
step 2: from 2 to 4
step 3: from 4 to 6 (no wait, the array is only 4 items long)
You can make this work with different strategies, and different loop parameters. But you ultimately need to figure out the above first, before you should even think about starting to write code.
@jsdisco I don’t think you know enough to make that judgement as to what I do. Theres actually a ton of posts I made so you can look into it if you want. I comment my thought process, look up the functions I need, then make an attempt. Once I can’t come to a solution I ask for help.
Right below I even commented what I tried to do
Be very careful about negative comments. Even if that’s not your intent, it’s not productive. Personally I think reading @nibble say “looks like you’re off by a mile” after after an hour attempt to solve a problem wasn’t the best either.
Since none of you have a magic wand that determines the effort someone puts in code, don’t jump to conclusions. I personally don’t care but there’s a ton of people who just quit coding when they get discouraged. Or maybe continue without posting questions like the guy who commented here he’s coded for a year and wants to quit.
Wow what a guilt trip. I don’t think I can help you beyond
slice doesn’t change the original array. If you want to slice something out of an array and put it somewhere else, you’ll have to assign it to a new variable
slice needs the correct indices (numbers), which you can retrieve from combining the i index of the for loop and the size given (this depends on the intended logic of your for loop)