Blog

JavaScript: Numbers

One thing I love about JavaScript is the fact that it has one numerical data type for holding numbers and its called simply Number. Internally, Number represents a 64-bit floating point thatĀ holdsĀ all possible number values one might need. By having a single numerical data type, JavaScript avoids common programming problems such as type conversion errors, loss of accuracy, and overflow problems.

'use strict';

// JavaScript has a single type to hold all numbers
//  1. Number: is internally represented as a 64-bit floating point
//  2. No separation of an Integer type => 1 === 1.0
//  3. Reduces number type errors
//  4. Completely avoids overflow problems in short integers
function compareNumbers() {
    return 1 === 1.0 ? '1 equals 1, no doubt about it' : 'Um, that\'s not supposed to happen.';
}
console.log(compareNumbers());

// Crazy number operations can return strange results
//  1. NaN => is a number value that is the result of an operation that cannot produce a normal result
//  2. NaN => is not equal to any value, including itself
//  3. You can only detect NaN using the function: isNaN(number)
console.log(Number.isNaN(NaN));
console.log(Number.isNaN(Number.NaN));

var nan_result = 0/0;
console.log(Number.isNaN(nan_result));
console.log(nan_result);

// Extremely large numbers
//  1. Infinity => represents all values greater than 1.79...e+308

// JavaScript has a Math object which contains methods which act on Numbers
var some_number = 12343.343;
function floor_it(some_number) {
    return Math.floor(some_number);
}
console.log(floor_it(some_number));

// Number class Constants
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;

JavaScript: Strings

If you are familiar with other programming languages, such as C# and Java, JavaScript strings should be a walk in the park as the are extremely similar (see example code below).

'use strict';

// JavaScript Strings are immutable
//  1. Once created they cannot be changed
//  2. Two Strings can be concatenated using the + operator
//  3. the '\' forward slash is the escape character to allow restricted characters

var string_one = "Some string with an escape character\" so that a paren can be in the string ";
var string_two = 'Another string using single quotes with an \' escaped single quoted';
var combined_string = string_one + string_two;

console.log(string_one);
console.log(string_two);
console.log(combined_string);

JavaScript: All About Functions

First Class Objects

Functions within the JavaScript (JS) language are saidĀ to be “First Class Objects”, this means they are comparable to objects composed from classes in C# or Java. The difference however is that classes (object templates) do not exist in the JSĀ language. Instead, JSĀ objects are created through the replication of already existing prototype objects provided by the language, but this is a topic for another day. Unlike C# and Java objects, JSĀ functions can accept parameters and contain executable code, when invoked that code can then be executed. This executable behavior is similar to methods in C# or Java, the only difference is C# or Java methods must exist within an object.

Named &Ā Anonymous

Another feature of JS functions is that they can be either named or anonymous. Named functions receive a name by which they can be referenced at the time they are defined. Anonymous Functions on the other hand are not provided names in their definitions, instead they are assigned to a variable and referenced in that manner throughout aĀ script.

"use strict";

// JavaScript Functions: First Class Objects
//  1. All functions in JavaScript are first class Objects
//     they do not have to exist inside an object
//  2. Functions all have Function.prototype as their Prototype Object
//  3. Unlike other Objects in JavaScript, Functions can be invoked
//  4. Functions can be stored in variables, objects, and arrays

// Named Function
// (referenced via the functions actual name)
function myFirstClassFunction(param){
    return "You passed me: " + param;
}
console.log(myFirstClassFunction("a Horse"));

// Anonymous Function
// (referenced via variable to which it is assigned)
var myAnonymousFunctionReference = function(param){
    return "You passed me: " + param;
};
console.log(myAnonymousFunctionReference("a Pillow"));

Passed as Arguments & Returned by Function

Because JS functions are objects and can be referenced they can also be passed around by reference. This means that JavaScript allows functions to be passed around a script as parameters to other functions to be used and executed by the receivingĀ function’s executable code. Also, functions can be defined and set up by a ‘parent’ function and returned by that function as its result of execution. The passing of functions asĀ arguments and the ability to return them is a feature of JavaScript that is not found in C# or Java. The closest thing to this in either language is a delegate, which is a special class that represents a method and can therefore be passed around.

'use strict';

// Passed as Arguments & Returned by Function
//  5. Can be passed as arguments to other Functions
//  6. Can be returned from other Functions
function iAcceptFunctionsAsParameters(param, func){
    return func(param);
}
console.log(iAcceptFunctionsAsParameters("a Bus", cool_function));


function iReturnFunctions(){
    return function (param){
        return "You passed me: " + param;
    };
}
// Obtaining reference to returned function
var returnedFunctionReference = iReturnFunctions();
console.log(returnedFunctionReference("a Bird"));

State &Ā Behavior

In conjunction, JS functionsĀ can also have their own state (variables, data) as well as behaviorĀ (methods) to support their execution or to provide additional functionality specific to the owning function. This is very similar to C# and Java objects that receive variables and methods from the class definition from which they were created.

'use strict';

// State & Behavior
//  7. Functions are Objects and can therefore have Variables & Methods (functions) of their own
var functionWithStateBehavior = function(){
    return "I am the core functionality";
};

// Add a variable to the function
functionWithStateBehavior.state = "I am a variable property on the 'functionWithStateBehavior' function.";

// Add a method to the function
functionWithStateBehavior.behaviorMethod = function(){
    return "I am the extra Method on a Function";
};

console.log(functionWithStateBehavior());
console.log(functionWithStateBehavior.state);
console.log(functionWithStateBehavior.behaviorMethod());

Mint 14: Latest Gedit Version + Ruby Plugins

If you are trying to use new plugins for Gedit, the default text editor on Mint, you might run into a problem as a lot of the new plugins require Gedit >=3.0 and be default Mint comes with 2.30.

To fix this you need to first get rid of Gedit 2.30 completely by running the following commands in your terminal.

sudo apt-get purge gedit
sudo apt-get purge gedit-common

Once those commands have completed successfully you need to run these commands to get the 3.6.1 version.





sudo apt-get install gedit-common/quantal
sudo apt-get install gedit/quantal

Now that we have the correct Gedit version we can install the Ruby and Rails plugins for Gedit. That’s all there is to it!

$ sudo apt-add-repository ppa:ubuntu-on-rails/ppa
$ sudo apt-get update
$ sudo apt-get install gedit-gmate

$ sudo apt-get install gedit-plugins

Mint 14/PostgreSQL: Peer authentication failed for user "someuser"

After you have installed PostgreSQL 9.1 on your Linux Mint 12/13/14, created a user login, a database with that user as the owner and try to connect to that database using that user, you may run into the following error:
Password for user someuser:
psql
: FATAL: Ā Peer authentication failed for user “someuser”

If this is the case, you need to open up this file /etc/postgresql/9.1/main/pg_hba.conf in a text editor (with root privileges) and change this line:

local Ā  all Ā  Ā  Ā  Ā  Ā  Ā  all Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  peer

to this:

local Ā  all Ā  Ā  Ā  Ā  Ā  Ā  all Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  Ā  md5

After changing and saving the file, restart the PostgreSQL server:

sudo service postgresql restart
You should not have no issue connecting to your databases using the associated user.

Mint 13: Installing Oracle JDK 7

How to install Oracle JDK 7 instead of the OpenJDK on your Linux Mint 13 (or Ubuntu 12) system. I found that I needed the official JDK in order to properly run JetBrains’ RubyMine IDE as well as a few other applications which do not play well with OpenJDK.
To do this open a terminal and execute the following commands in order:
1
2
3
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java7-installer
After the installation runs its course, check to make sure that Java is reporting the correct version of JDK 7. Execute the command:
1
java -version
It should return something like this:
1
2
3
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
If the version reported back is not the latest as installed by the above command you may need to force the system to update its references to Java and its executables. Run the following command and then check the version again to verify:
1
sudo update-java-alternatives -s java-7-oracle