Question on Chunky Monkey (spoilers)

I’m wondering exactly what this line is doing in the basic solution: if (a % size !== size - 1)

I know this is modular arithmetic but why is it being used here? Can somebody give an example?

1 Like

We breaking this large array into smaller chunks of size size. We are stepping through arr step by step with the iteration variable a. We will push items into temp until there are size in number. We we need a way to check when temp is full with the correct number. This code checks if it is not full, the else handles if it is almost full (pushes the last one on, pushes that subarray onto result, and empties temp)

   if (a % size !== size - 1)
      temp.push(arr[a]);
    else {
      temp.push(arr[a]);
      result.push(temp);
      temp = [];
    }

So, that evaluation is what is important. Whenever I am confused about how something works, I just start console.loging things.

function chunkArrayInGroups(arr, size) {

  var temp = [];
  var result = [];
  console.log('\n\n');
  for (var a = 0; a < arr.length; a++) {
    console.log('*** a = ', a);
    console.log('(a % size) = ', (a % size), '   and (size-1) = ', (size-1))
    if (a % size !== size-1) {
      console.log('temp is not full, push element');
      temp.push(arr[a]);
    } else {
      console.log('temp is almost full, push last element...');
      console.log('... then push temp to result and clear temp')
      temp.push(arr[a]);
      result.push(temp);
      temp = [];
      console.log('\n\n');
    }
  }

  if (temp.length !== 0)
    result.push(temp);
    
  console.log('\n\n')
  
  return result;
}

chunkArrayInGroups([1, 2, 3, 4, 5, 6, 7], 3);

Work with that. Change the numbers around. Track how it works.

Thank you for the helpful explanation and code. There’s something about using the remainder operation to test the capacity of the array that I still can’t wrap my head around. It’s one of those solutions that looks great but that I wouldn’t come up with on my own. I don’t understand the relationship between a%size and size-1 and why that would be a reliable indicator.

There’s something about using the remainder operation to test the capacity of the array that I still can’t wrap my head around.

It’s not testing the capacity of the array, it’s keeping track of how many elements have been put into the subarrays.

Think of it this way. You are working on a cruise ship (I used to, so this is an easy metaphor for me). The ship hits an ice berg. Your job is to stand at the life boat launch (for some reason there is only one station to launch all the life boats) with a clicker, to keep count.

Each lifeboat only holds 50 people. So you click away on your clicker and keep track of how many people have been loaded. If the number is not one less than being evenly divisible by 50 (i%sizeBoat !== sizeBoat-1) then keep loading people. When it is one less than being evenly divisible by 50, push the next person on, launch that boat, and get the next boat ready to go.

That’s essentially what you’re doing here. The variable a is your clicker and temp is your lifeboats, and you “launch” them once they are full by pushing them into result.

1 Like