var and let the difference in JavaScript

Javascrit has something called Hoisting. This is when all variables and functions are loaded before the program runs.

function loadProfiles(userNames){
// some code...

    var loadingMessage = "Hold a sec... Profiles loading!";
// some code...
}

var loadingMessage = "Loading your profile!";

What will happen here is that the first declaration of loadingMessage will be loaded first,then the second declaration will be loaded and overwrite the first. Which will not give us what we want.
To solve this issue ES6 or ES2015 came with a solution: let !

function loadProfiles(userNames){
// some code...

    let loadingMessage = "Hold a sec... Profiles loading!";
//some code...

}

let loadingMessage = "Loading your profile!";

These variables will load locally, saving memory without overwriting each other. If you console.log the loadingMessage variable after the loadProfiles method, the result will be different from if you ran inside the loadProfiles scope.
What is a scope? I’ll tell you later.

Hexadecimal and Colors

This week will be more related to the Front End side of web development. So I thought about colors which are used everywhere. The web is one of the technologies that uses all the human senses, it evolved to become an everyday tool and entertainment.
Today I will teach you how to calculate hexadecimal numbers and how they can be related to numbers.

Hexa-decimal: Hexa comes from the Greek word héx which means six. The decimal I’m sure that you already now.
Decimal numbers run from 0-9.

81 (decimal):
multiply the left by 10, (8x10)
multiply the right by 1, (1x1)
We get 81 decimal.
81 (hexadecimal):
multiply the left by 16, (8x16)
multiply the right by 16, (1x1)
By adding them we get 129 hexadecimal!

When we work with colors particularly RGB, the minimum value is 0 and maximum is 255.
With hexadecimal numbers we work with 3 pairs of Red, Green and Blue, hence RGB.
_ _  _ _  _ _
R     G     B

The minimum pair is 00 and maximum FF. Oh! Letters? Yes, I will show you in a bit how this works. As I said above the decimal numbers are limited to 0-9, so a few engineers had to figure out a way to solve the problem. To get things working they assigned the following values:

A = 10, B = 11, C = 12, D = 13, E = 14, F = 15

Few examples:

#FF0000
Red: (15x16) + (15x1) = 255
Green: (0x16) + (0x1) = 0
Blue: (0x16) + (0x1) = 0
Yes, you got it right, the color is full red! My favorite color.

#83d900
Red: (8x16) + (3x1) = 131
Green: (13x16) + (9x1) = 217
Blue: (0x16) + (0x1) = 0

#efcc3a
Red: (14x16) + (15x1) = 239
Green: (11x16) + (11x1) = 187
Blue: (3x16) + (10x1) = 58

And that’s it! You know how to calculate hexadecimal number and how they are related to numbers.

What are Programming Languages ?

2 weeks ago I went to an Equal Experts Conference and after while mingling, in a conversation with one of the organizers, some were saying that language X was better than language B, and his response was: “Languages are just tools to build applications to serve the world”. That just killed.

And this is the message about this post, Programming Languages are just tools, you use them and not get used by them.

Have a nice day.

How to Print Urls with Nokogiri

It has been a while since I made a post here, the excuses will be off and I’ll just get to the post.

I will show a way of printing a url by building a Ruby script with Nokogiri.
What I want is to print all the compressed files in the website, these are contained inside the td (table data) of tr (table rows).

require 'nokogiri'
require 'open-uri'

class Scraper
  def initilize(url)
    @url = url
  end

  def file_url
    page = Nokogiri::HTML(open(@url))
    rows = page.css('table tr')

    rows[1..-2].each do |row|
      hrefs = row.css("td a").map { |a|
        a['href'] if a['href'].match(".tar.gz")
      }.compact.uniq

      hrefs.each do |href|
        puts @url + href
      end
    end
  end
end

scrap = Scraper.new('https://cran.r-project.org/src/contrib/')

scrap.file_url