Browse Month

September 2014

Quirky JavaScript Object Access

My friend asked me a simple JavaScript question last night… or so I thought.

The premise is that we have a javascript object like this:

var object = {
  1: {
    items: ['a','b','c']
  }
}

Now the question is, how do you access that items  array?

From what I know:

  • An object’s keys are stringified so that they are strings.
  • Using object.X  is the same as writing object[‘X’] .

So, I told my friend that we can access that items  array by using object.1.items  or object[‘1’].items .

However, object[‘1’].items worked, while object.1.items threw an error! To my surprise, object[1].items  also worked!

Now let’s say the javascript object is like this:

var object = {
  one: {
    items: ['a','b','c']
  }
}

In this second case, I tested it in the Chrome browser console and confirmed that it is how I originally thought it would work: object[‘one’].items  and object.one.items  both work (and object[one].items  does not work).

I think I will just use the object[‘X’]  format since it works in both cases: when the key is a number and when it is a word.

I still don’t know the reason for the quirkiness when the key is an integer, so feel free to leave a comment and let me know!

Pigeonhole Sort

Pigeonhole sort is an interesting sorting algorithm that makes use of an array filled with zeros and its indices. It can sort values in linear time and is relatively simple to implement. However, the tradeoff for its sorting speed is the space that it requires. Also, since it makes use of array indices, only positive integers can be sorted using this algorithm.

So the basic idea of pigeonhole sort is:

  • Create a new array (let’s call it pigeonholeArray) and fill it with zeros.
  • Iterate through the unsorted user’s array (inputArray).
  • For each value of the inputArray, add 1 to the index corresponding with the value in the pigeonholeArray.

Let’s say for this example, we want to sort this inputArray:

// inputArray:
[5, 2, 7, 5, 1]

The first thing the algorithm needs to know is what the maximum value of the inputArray is. We need to create a pigeonholeArray that has indices that go up to the max value. If we don’t know the maximum, we’ll have to loop through the inputArray one time to figure it out. In this case, our biggest value is 7, thus we need to create a pigeonholeArray that can hold 8 elements (which will have indices 0 to 7).

// pigeonholeArray:
[0, 0, 0, 0, 0, 0, 0, 0]

Now we need to loop through the unsorted inputArray (again), and as we do that, we will add 1 to the pigeonholeArray’s value at the pigeonholeArray’s index corresponding with the inputArray’s value. So as we iterate our input array, our pigeonhole array will look like this:

// Progress of pigeonholeArray
[0, 0, 0, 0, 0, 1, 0, 0] // when the inputArray value is at 5
[0, 0, 1, 0, 0, 1, 0, 0] // when the inputArray value is at 2
[0, 0, 1, 0, 0, 1, 0, 1] // when the inputArray value is at 7
[0, 0, 1, 0, 0, 2, 0, 1] // when the inputArray value is at 5
[0, 1, 1, 0, 0, 2, 0, 1] // when the inputArray value is at 1

As you can see, after looping through the entire inputArray, our pigeonholeArray will contain all of inputArray’s values. We will just have to make one last iteration through the pigeonholeArray. For each pigeonholeArray’s value that is greater than zero, we’ll add the pigeonholeArray’s index to the inputArray. We can replace the old values in the inputArray so that no new array needs to be created.

Check out the completed algorithm below:

function pigeonholeSort(inputArray) {
  // Find the max value in inputArray
  var max = inputArray[0];
  for (var i = 1; i < inputArray.length; i++) {
    if (max < inputArray[i])
      max = inputArray[i];
  }

  // Create pigeonholeArray filled with zeros
  var pigeonholeArray = [];
  for (var i = 0; i <= max; i++) {
    pigeonholeArray.push(0);
  }

  // Iterate through inputArray and add 1's to pigeonholeArray
  for (var i = 0; i < inputArray.length; i++) {
    pigeonholeArray[inputArray[i]]++;
  }

  // Iterate through the pigeonholeArray and replace values in inputArray
  var inputArrayIndex = 0;
  for (var i = 0; i < pigeonholeArray.length; i++) {
    // Add to inputArray until the current pigeonholeArray index is 0
    while (pigeonholeArray[i]) {
      inputArray[inputArrayIndex] = i;
      pigeonholeArray[i]--;
      inputArrayIndex++;
    }
  }

  // return sorted inputArray
  return inputArray;
}