Chunky Monkey- Help with algorithim/pseudo [solved]

Chunky Monkey- Help with algorithim/pseudo [solved]
0.0 0

#1

Tell us what’s happening:
Hi There FCC,

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:

  1. Create empty array
  2. Determine length of each sub array
  3. Use for loop to cut the given array length determined by the input
  4. Push the sliced array into the new array
  5. Increment loop based on size
  6. 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.

Link to the challenge:


#2

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)

#3

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?


#4

The push function always returns the length of the array after the new element is added. If you do not assign this returned value to anything, it just does not get assigned to anything but the addition of the new element still happens by default.


#5

Thanks for the clarification :slight_smile:


#6

You can also try to implement this function:
https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/seq.groupby['t,'key]-function-[fsharp]
then use it to solve this challenge.