Wednesday, March 14, 2018

FreeCodeCamp intermediate algorithms

I recently started FreeCodeCamp. Okay, not recently. I have been taking my time completing the challenges and tasks, but it has been pretty good. I enjoy learning the JavaScript and I am hoping I begin understanding everything more. I will be honest, I have been going slow because I am coding all day at work using Angular. Angular is great and very challenging, so why do I continue doing FreeCodeCamp?

Perhaps I feel that it will give me a foundation on the background of Angular, after all Angular uses JavaScript, right? Maybe it will help with debugging? Plus, I started FreeCodeCamp before I got hired to my team that uses Angular, and I'm not a quitter. I am to the Intermediate Algorithm Scripting and, as always, I am finding many different ways to complete the tasks they as you to do. I know that I am not the most efficient coder and there are probably much better ways to code the tasks.

I would like to go back and look at all of the tasks I have already done to see if there is something I could do better, but who knows when/if I will find time to do it. But let's start on the challenge called "Diff Two Arrays". The task seems easy enough, compare to arrays and return a new array that only contains objects that are in just one of the original arrays.

For example
arr1: [1,3,4,5]
arr2: [1,2.5,6]

My call to diffArray should return
return: [2,4,6]

My first time around I used a for statement to check each object in array1 to see if array2 included that object. Then I did the same for array2. Not the most efficient way, I know, but it worked and it was my original thought (I usually try to do the task without using the hints to see if I am able to complete the task on my own free will).

function diffArray(arr1, arr2) {
  
var newArr = [];

for(var i = 0; i < arr1.length; i++) {
   if (!arr2.includes(arr1[i])) {
     newArr.push(arr1[i]);
   }
}
  
  for(var t = 0; t < arr2.length; t++) {
   if (!arr1.includes(arr2[t])) {
     newArr.push(arr2[t]);
   }
  }
 return newArr;
}


It get's the job done. I know that I use arrow functions at work to pull data from a database, so I thought why wouldn't that work here? FreeCodeCamp mentions that you can use Array.prototype.filter() so I went to explore this in the JavaScript context. I'm going to be honest, it took me a bit to figure out how to use arr1.filter and .concat(arr2.filter). But in the end the code is much cleaner. (although it was unnerving when the FreeCodeCamp gave me a warning: " 'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6')"


function diffArray(arr1, arr2) {
  
    return arr1
      .filter(ai => !arr2.includes(ai))
      .concat(
        arr2.filter(ai => !arr1.includes(ai))
      );
}

A much simpler solution. The filter takes from each array and will remove any object that is included in the second array. Then you use concat to add the filter from arr1 to the filter from arr2.

Sometimes our first response will get it working but sometimes we need to look a bit to find a better solution.

I will post some more of these, I have plenty of examples.


"Well done is better than well said." - Benjamin Franklin

No comments: