RSS

Values regarded as conditional false in Javascript

These values are treated as false in conditional statements, all other values including the string ‘false’ are treated as true:

Value Type
0 Number
NaN Number
”(empty string) String
false Boolean
null Object
undefined Undefined

For example, the code below will alert “NaN is falsy”: Read the rest of this entry »

Advertisements
 
Leave a comment

Posted by on September 13, 2012 in Javascript, jQuery

 

this, $(this), $this and that!

These terms may confuse beginners in learning jQuery.

this in a javascript function closure refers to the object invoking that function, for example:

var target = {
    getObject: function () {
        return this;
    }
};

alert(target.getObject());    // target object

Read the rest of this entry »

 
Leave a comment

Posted by on August 30, 2012 in Javascript, jQuery

 

Handling keypress event in jQuery

To determine which key is pressed by the user, simply check jQuery event.which property for the keycode. The example below binds textarea elements with keypress event and cancel the default Enter key function:

$("textarea").keypress(function(e) {
    // If the key pressed is Enter
    if ( e.which === 13 ) {
        // Disable line break
        e.preventDefault();

        // Do things

    }
});

Here are 2 useful links to look up javascript keycode:
Javascript Char Codes (Key Codes) – Cambia Research
JavaScript Event KeyCode Test Page

 
Leave a comment

Posted by on August 28, 2012 in Javascript, jQuery

 

Randomized Quick Sort in C#

Instead of choosing the last element of every sub array as the pivot, we choose a random element in Randomized version and swap it with the last element before partitioning.

public static void RandomizedQuickSort(int[] input, int left, int right)
{
    if (left < right)
    {
        int q = RandomizedPartition(input, left, right);
        RandomizedQuickSort(input, left, q - 1);
        RandomizedQuickSort(input, q + 1, right);
    }
}

Read the rest of this entry »

 
Leave a comment

Posted by on August 22, 2012 in Algorithm, C#

 

Quick Sort in C#

A simple Quick Sort implementation using C#. In this version the pivot is the last element in every sub array.

public static void QuickSort(int[] input, int left, int right)
{
    if (left < right)
    {
        int q = Partition(input, left, right);
        QuickSort(input, left, q - 1);
        QuickSort(input, q + 1, right);
    }
}

Read the rest of this entry »

 
3 Comments

Posted by on August 21, 2012 in Algorithm, C#

 

Heap Sort in C#

A simple Heap Sort implementation using C#.

public static void HeapSort(int[] input)
{
    //Build-Max-Heap
    int heapSize = input.Length;
    for (int p = (heapSize - 1) / 2; p >= 0; p--)
        MaxHeapify(input, heapSize, p);

    for (int i = input.Length - 1; i > 0; i--)
    {
        //Swap
        int temp = input[i];
        input[i] = input[0];
        input[0] = temp;

        heapSize--;
        MaxHeapify(input, heapSize, 0);
    }
}

Read the rest of this entry »

 
8 Comments

Posted by on August 21, 2012 in Algorithm, C#

 

Merge Sort in C#

A simple Merge Sort implementation using C#.

public static void MergeSort(int[] input, int left, int right)
{
    if (left < right)
    {
        int middle = (left + right) / 2;

        MergeSort(input, left, middle);
        MergeSort(input, middle + 1, right);

        //Merge
        int[] leftArray = new int[middle - left + 1];
        int[] rightArray = new int[right - middle];

        Array.Copy(input, left, leftArray, 0, middle - left + 1);
        Array.Copy(input, middle + 1, rightArray, 0, right - middle);

        int i = 0;
        int j = 0;
        for (int k = left; k < right + 1; k++)
        {
            if (i == leftArray.Length)
            {
                input[k] = rightArray[j];
                j++;
            }
            else if (j == rightArray.Length)
            {
                input[k] = leftArray[i];
                i++;
            }
            else if (leftArray[i] <= rightArray[j])
            {
                input[k] = leftArray[i];
                i++;
            }
            else
            {
                input[k] = rightArray[j];
                j++;
            }
        }
    }
}
 
3 Comments

Posted by on August 21, 2012 in Algorithm, C#