Tuesday, July 10, 2012

Badly written js function

// this function is not well written
// it is trying to find the last value in a linked list
// The linked list is held in an array where each item of the linked list is an array of size 2
// The first item in the list is an index to the next item in the list.  The list terminates if the index is -1
// The second item in the list is the value
// Consider the following examples:
//  Given the following array, Array ( Array(1, 27), Array(-1, 12) ), the function will return 12
//  Given the following array, Array ( Array(2, 27), Array(-1, 12), Array(-1, 18) ), the function will return 18
//  Given the following array, Array ( Array(2, 27), Array(-1, 12), Array(1, 18) ), the function will return 12

function findLastValueInLinkedList(a)
{
    b = 0;
    while (1 == 1) {
        if (a[b][0] == -1)
            return a[b][1];
        b = a[b][0];
    }
}