DEV Community

Christian Falucho
Christian Falucho

Posted on • Updated on

Day 3 of #100DaysOfCode!

Today's progress😌

I completed Basic Algorithm Scripting from freeCodeCamp and let me tell you. It was challenging. I probably spent a good 30-40 mins on one problem statement.

What I learned

Read up JavaScript documentations from MDN and w3schools. Both resources has been very helpful in learning more about string methods, arrays and just about anything you need to learn. The examples have been quite helpful to get a better idea of what is happening with the code.

I want to talk about a basic algorithm problem I had to solve and how I broke it down.

Problem Statement - Chunky Monkey

Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.

function chunkArrayInGroups(arr, size) {
  let newArr = [];

  for(let i = 0; i < arr.length; i = i + size){
      // 0 
      // 2 = 0 + 2
      // 4 = 2 + 2

      newArr.push((arr.slice(i, i + size)))
      // 0, 2
      // 2, 4
      // 4, 6
  }
  return newArr;
}

chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)
Enter fullscreen mode Exit fullscreen mode

Let's begin with the function. Below is our function where we take two arguments, an array and size. The size is going to indicate how many elements per sub-array. For instance, [0, 1, 2, 3, 4, 5], 2 => [[0,1], [2,3], [4,5]]

function chunkArrayInGroups(arr, size) {
}
Enter fullscreen mode Exit fullscreen mode

Next, we are going to create a new variable where we will store elements in sub-arrays.

let newArr = [];
Enter fullscreen mode Exit fullscreen mode

Now, we need to loop through the array. So we are going to create a for loop and iterate through each element. For our increment expression we are going to increment by adding our size argument.

for(let i = 0; i < arr.length; i = i + size;){
                            // i = i + size
                            // 0
                            // 2 = 0 + 2
                            // 4 = 2 + 2 
}
Enter fullscreen mode Exit fullscreen mode

Let's go ahead and use the slice() method to return and extract elements from the array. We can take in two parameters with the slice() method, slice(start,end)

arr.slice(i, i + size)
// slice(0, 0 + 2) returns [0,1]
// slice(2, 2 + 2) returns [2,3]
// slice(4, 4 + 2) returns [4,5]

// returns elements 
Enter fullscreen mode Exit fullscreen mode

We want to push the return elements into our a new array. We can do this by using the push() method

newArr.push(arr.slice(i, i + size))
Enter fullscreen mode Exit fullscreen mode

Putting it all together.

function chunkArrayInGroups(arr, size) {
  let newArr = [];

  for(let i = 0; i < arr.length; i = i + size){
      newArr.push((arr.slice(i, i + size)))   
  }  
  return newArr;
}
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)

// output:  [[0,1], [2,3], [4,5]]

Enter fullscreen mode Exit fullscreen mode

I broke down every line of code and took my time to understand what each code was doing. This helped me to be able to put it all together at the end.

Overall, a productive coding day🙌!

Top comments (0)