Wednesday, March 28, 2018

Javascript Variables

In C#, a method required you to declare the number of arguments and each arguments data type.If you declare a method requires an argument your code will not compile unless the argument is given each time the method is called. JavaScript works a little different.

JavaScript does not require you to specify the data types for each argument. It also does not perform any type of check on the arguments when the function is called. You can pass multiple arguments, or no arguments. If you declare an argument and the function receives nothing JavaScript will assume undefined for the missing arguments.

FreeCodeCamp had an algorithm where arguments were sent in different ways. For example, addTogether(2, 3) would return 5, just the sum of the two arguments passed.

 if(args.length == 2){
    if(typeof(args[0]) !== 'number' || typeof(args[1]) !=='number' ){
      return undefined;
      }
    return args[0]+args[1];
   }

But, what if you received something like addTogether(2)(3)? This confused me (and still kind of does), two sets of arguments? How do you deal with that?

I did some reading and found that it is a valid call to a function. Now I needed to figure out how to access that second variable.

It turns out that while looking at the first variable you can create another function that will look at the second variable.

if(args.length == 1){
     firstSet= args[0];
     
    if(typeof firstSet!=='number'){
        return undefined;
      }
   
    else{
       return function(secondSet){
         if(typeof secondSet !=='number'){
           return undefined;
           }
         else
            return firstSet+secondSet;
          };
      }
    }

Good to know, I'm just wondering when I will use this in real life...why would you send two sets of variables instead of just one set or an array?


"Sooner or later, those who win are those who think they can." - Paul Tournier


Wednesday, March 21, 2018

Smallest Common Multiple - using Euclid's method

I spent some time trying to figure out how I was going to solve the smallest common multiple. There are multiple ways to find this number. You can write down all the multiples and just look, you can use a grid/ladder method, prime factorization, or Euclid's method (the method I selected). If you want to see each method you can go here and see the how to accomplish through the different methods.

I decided to try and use Euclid's method. Math is used to find the greatest common divisor which is then used to find the least common multiple. Euclid's method requires you to perform the same calculation multiple times until you have a remainder of zero. You could do a while statement that looks at the remainder and stops when the remainder is zero. I also looked at a different way to do this, using recursion.

I learned about recursion a while back and figured I would give it a try here. I had a definite stopping point and needed to run the same calculations over until I got a certain number. It took me a few tries at getting the recursion to work (knowing what to put where), but I eventually landed on the below code:

function euclid(high, low) {
if(low === 0) {
return high;
} else {
return euclid(low, high%low);
}
}

I call the function and pass it the first number in an array as the high number and the second number as the low number. It then goes into the function to see that low is not at zero so it calls itself with the low number as high and the remainder of high divided by low (you use modulus to return only the remainder as the new low number. Once the modulus returns a zero, return the high number.

Euclid's method then takes the product of the first original two numbers and divides it by the number returned from our Euclid function. This will give you the smallest common multiple for the two original numbers.


"Laziness may appear attractive, but work gives satisfaction." - Anne Frank

Friday, March 16, 2018

Key Value Pair Statements - Better for the eyes, but better performance?

There have been two challenges on Free Code Camp now that I went down the road of using if statements. It started getting ugly with all those if and else if statements. You could use a case statement for each of the if statements, the case statement could possibly be easier to read and possibly have a faster performance.

Below is an example for possible DNA strings:

function pairElement(str) {
var myArray = str.split('');
var returnArray = [];
for (st in str) {
if(myArray[st] == 'G') {
returnArray.push([myArray[st], 'C']);
}
else if(myArray[st] == 'C') {
returnArray.push([myArray[st], 'G']);
}
else if(myArray[st] == 'A') {
returnArray.push([myArray[st], 'T']);
}
else {
returnArray.push([myArray[st], 'A']);
}
}

But then I got thinking, what if there were a dictionary or map that would allow someone to look up the index and see what the value output is for the item. I believe I had seen something like that before so I started searching. Turns out a map is exactly what I needed.

This technique allows me to create a map, or a key value pair, that will let me look up the value I am looking up and see it's matching string. Your first key value set G: 'C' will look up anytime your key is G it will output C.

By splitting the input string by their characters, one you are then able to use that item as the lookup for your key and return the value you need.

function pairElement(str) {
  var pairMap = {G:'C', C:'G', A:'T', T:'A'};
  var myArray = str.split('');
  
  for(var st in str) {
    myArray[st] = [myArray[st], pairMap[myArray[st]]];
  }
  
  
  return myArray;
}

The code is much easier to read and I am guessing it will have better performance as it doesn't have to check each if/else if statements.

"Nothing is work unless you'd rather be doing something else." - George Halas

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