CS133J JavaScript Archives

Wednesday
Nov302011

The Real Lab 7 Solution

Tuesday
Nov152011

Google

Yesterday we touched a little on how Google ranks pages for its search results. Here's an article where they open the kimono a little further...

Sunday
Nov132011

This Is Too Cool Not To Share

Thursday
Nov102011

Sort Demo From Class

Here's the code we wrote in class today for performing a case-insensitive sort on an array of strings.

var a = ['ant', 'cat', 'Dog', 'Bug'];

a.sort(function(a,b) {
    x = a.toLowerCase();
    y = b.toLowerCase();
    if(x < y) return -1;
    if(x > y) return 1;
    return 0;
});

alert(a);
Thursday
Nov102011

Multidimensional Arrays

The question of multidimensional arrays came up in class again yesterday and I wanted to reiterate that JavaScript does not have true multidimensional arrays. Let's (literally) take a page from the experts, here's what JavaScript: The Definitive Guide, 6th Ed, (O'Reilly Media, Inc., ISBN: 978-0-596-80552-4) has to say:

7.7. Multidimensional Arrays

JavaScript does not support true multidimensional arrays, but you can approximate them with arrays of arrays. To access a value in an array of arrays, simply use the [] operator twice. For example, suppose the variable matrix is an array of arrays of numbers. Every element in matrix[x] is an array of numbers. To access a particular number within this array, you would write matrix[x][y]. Here is a concrete example that uses a two-dimensional array as a multiplication table:

// Create a multidimensional array
var table = new Array(10);               // 10 rows of the table
for(var i = 0; i < table.length; i++)
    table[i] = new Array(10);            // Each row has 10 columns

// Initialize the array
for(var row = 0; row < table.length; row++) {
    for(col = 0; col < table[row].length; col++) {
        table[row][col] = row*col;
    }
}

// Use the multidimensional array to compute 5*7
var product = table[5][7];  // 35
Tuesday
Nov012011

More on the Switch Statement

I said in class on Monday that you could neither mix string and numeric case clauses in switch statments nor next switch statements. I was wrong on both counts (JavaScript is much more flexible than I gave it credit for). Here's sample code that does exactly what I said yesterday wasn't possible.

var leftAlpha = document.getElementById('left').value;
var leftNum = parseInt(leftAlpha);
var rightAlpha = document.getElementById('right').value;
var rightNum = parseInt(rightAlpha);
  
var left = isNaN(leftNum) ? leftAlpha : leftNum;
var right = isNaN(rightNum) ? rightAlpha : rightNum;
  
switch(left)
{
  case 'A':
    switch(right)
    {
      case 'A':
        alert('You entered A & A');
        break;
      case 'B':
        alert('You entered A & B');
        break;
      case 1:
        alert('You entered A & 1');
        break;
      case 2:
        alert('You entered A & 2');
        break;
    }
    break;
  case 'B':
    switch(right)
    {
      case 'A':
        alert('You entered B & A');
        break;
      case 'B':
        alert('You entered B & B');
        break;
      case 1:
        alert('You entered B & 1');
        break;
      case 2:
        alert('You entered B & 2');
        break;
    }
    break;
  case 1:
    switch(right)
    {
      case 'A':
        alert('You entered 1 & A');
        break;
      case 'B':
        alert('You entered 1 & B');
        break;
      case 1:
        alert('You entered 1 & 1');
        break;
      case 2:
        alert('You entered 1 & 2');
        break;
    }
    break;
  case 2:
    switch(right)
    {
      case 'A':
        alert('You entered 2 & A');
        break;
      case 'B':
        alert('You entered 2 & B');
        break;
      case 1:
        alert('You entered 2 & 1');
        break;
      case 2:
        alert('You entered 2 & 2');
        break;
    }
    break;
  default:
    alert('bad input');
}
Monday
Oct312011

Goodies!

There's a lot of goodies on this page: some you may be interested in now and some to tuck away for later reference.

Sunday
Oct302011

Cool HTML5/JavaScript Site

Tuesday
Oct252011

Chapter 7 Demos

Wednesday
Oct192011

In-Class Demos (Really)

Now with 100% less broken!

The chapter 6 demos are here (honest)! Sorry for the delay.