Day 3 of #100DaysOfCode!

Tags: beginners, javascript, 100daysofcode, codenewbie
Published: 2021-06-19 04:53:08

<h3>Today's progress😌</h3>
I completed <a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/convert-celsius-to-fahrenheit">Basic Algorithm Scripting</a> from freeCodeCamp and let me tell you. It was challenging. I probably spent a good 30-40 mins on one problem statement.

<h3>What I learned</h3>
Read up JavaScript documentations from <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">MDN</a> and <a href="https://www.w3schools.com/jsref/jsref_substring.asp">w3schools</a>. 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.* 

```javascript
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)
```

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]]` 
```javascript
function chunkArrayInGroups(arr, size) {
}
```

Next, we are going to create a new variable where we will store elements in sub-arrays. 
```javascript
let newArr = [];
```
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. 
```javascript
for(let i = 0; i < arr.length; i = i + size;){
                            // i = i + size
                            // 0
                            // 2 = 0 + 2
                            // 4 = 2 + 2 
}
```

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)`
```javascript
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 
```

We want to push the return elements into our a new array. We can do this by using the `push()` method
```javascript
newArr.push(arr.slice(i, i + size))
```

Putting it all together.
```javascript
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]]

```
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. 

<h2>Overall, a productive coding day🙌!</h2>