I’m taking a new approach to learning after reading some forums and I’m strongly changing my approach by forcing myself to write pseudocode and trying to code it instead of just looking at solutions from googling (takes away the thinking). I would love criticism on my algorithim/code that’ll push me to solving the solution myself.
Thank you!
Algorithim:
Create empty array
Determine length of each sub array
Use for loop to cut the given array length determined by the input
Push the sliced array into the new array
Increment loop based on size
Return final array
Current issues:
Your code so far
function chunkArrayInGroups(arr, size) {
var newarray=[];
for (i = 0;i<arr.length;i+=size){
newarray = arr.slice(i,size).push();
return (newarray);
}}
chunkArrayInGroups(["a", "b", "c", "d"], 2);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0.
I think your pseudocode is good enough. You just need to translate it to JS code (and possibly make changes to it as you code).
A couple things to note:
The second argument to .slice() is not the length of the subarray, but the zero-based index before which to end extraction, excluding that index itself (doc).
You want to push to newarray, so it should do the pushing, not the sliced array. Also, be careful with assigning the result of push() to a variable (it returns the new length after pushing). In most cases, you don’t want to assign it to a variable (especially the variable that hold the array you’re pushing to!) (doc)
Thanks for your help! I really didn’t understand your comments but after trying rearranging code and modifying things I got it and now your wording makes sense.
So what you are saying for push is that if I assign it to a variable it returns length, but if i just use the object command it will perform and return the modified array?