Skip to content

Instantly share code, notes, and snippets.

@bmavity
Created February 11, 2011 17:00
Show Gist options
  • Save bmavity/822661 to your computer and use it in GitHub Desktop.
Save bmavity/822661 to your computer and use it in GitHub Desktop.
Keesus' two dimensional array
//HaloGame has the title of a particular halo release
HaloGame = new Array("Halo", "Halo 2", "Halo 3", "Halo: ODST", "Halo: Reach");
var haloYearDifferences = new Array (0, 3, 6, 8, 9);
var halo2YearDifferences = new Array (3, 0, 3, 5, 6);
var halo3YearDifferences = new Array (6, 3, 0, 2, 3);
var haloOdstYearDifferences = new Array (8, 5, 2, 0, 1);
var haloReachYearDifferences = new Array (9, 6, 3, 1, 0);
//creating a 2-dimensional array of years between releases
var timeSpan = new Array (
haloYearDifferences,
halo2YearDifferences,
halo3YearDifferences,
haloOdstYearDifferences,
haloReachYearDifferences
);
//creating a 2-dimensional array of years between releases
//timeSpan = new Array (
//new Array (0, 3, 6, 8, 9),
//new Array (3, 0, 3, 5, 6),
//new Array (6, 3, 0, 2, 3),
//new Array (8, 5, 2, 0, 1),
//new Array (9, 6, 3, 1, 0)
//);
function getGame()
{
//presents a list of Halo titles and gets a number corresponding
//to the chosen game
var theGame = ""; //will hold the game title
var gameMenu = "Please choose a game by typing a number: \n";
gameMenu += "0) Halo \n";
gameMenu += "1) Halo 2 \n";
gameMenu += "2) Halo 3 \n";
gameMenu += "3) Halo: ODST \n";
gameMenu += "4) Halo: Reach \n";
theGame = prompt(gameMenu);
return theGame;
} //end getGame
function main() {
var output = "";
var game1 = getGame();
var game2 = getGame();
//var result = timeSpan[game1][game2];
// allYearDifferencesForGame1 is also an array
var allYearDifferencesForGame1 = timeSpan[game1];
var yearDifferenceBetweenGame1AndGame2 = allYearDifferencesForGame1[game2];
output = "The time between " + HaloGame[game1];
output += " and " + HaloGame[game2];
output += " is " + result + " years.";
alert(output);
} //end main
main();
// Less involved example
var siblings = ['Bobby', 'Tony', 'Betsy', 'Kevin'];
var tony = siblings[1];
// tony now has the value of the item at index 1 : a string 'Tony'
var parents = ['Tony', 'Dee'];
var family = [siblings, parents];
// family is now a 2 dimensional array
var firstItemInFamily = family[0];
// firstItemInFamily is the exact same thing as siblings (so it's an array)
var you = firstItemInFamily[3];
// you is now the string 'Kevin'
// the shortcut you are using in the above code is family[0][3] which combines the two above steps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment