Core JavaScript

Multidimensional Arrays

Multidimensional arrays are created in JavaScript by the nested application of initializers or dynamic allocations. In the program below, I have created 2 two-dimensional arrays. The first is created with nested initializers and holds the books of the Bible from the Old Testament. The second is created with nested dynamic allocations and holds the books of the Bible from the New Testament.

The strings in the arrays are of different sizes. So, I use the length of the strings to caluate how many spaces need to be added for padding. The spaces are added via the non-breaking space (nbsp) entity. Additionally, I styled the body tag with the courier new font to ensure that the characters all have the same width.

MultidimensionalArrays.html

<!DOCTYPE html>
<html>
<head>
    <title>XoaX.net's Javascript</title>
</head>
<body style="font-family: courier new">
    <script type="text/javascript" src="MultidimensionalArrays.js"></script>
</body>
</html>

MultidimensionalArrays.js

var saaOldTestament = [["Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy"],
	["Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel"],
	["1 Kings", "2 Kings", "1 Chronicles", "2 Chronicles", "Ezra"],
	["Nehemiah", "Tobit", "Judith", "Esther", "1 Maccabees"],
	["2 Maccabees", "Job", "Psalms", "Proverbs", "Ecclesiastes"],
	["Song of Songs", "Wisdom", "Sirach", "Isaiah", "Jeremiah"],
	["Lamentations", "Baruch", "Ezekiel", "Daniel", "Hosea"],
	["Joel", "Amos", "Obadiah", "Jonah", "Micah"],
	["Nahum", "Habakkuk", "Zephaniah", "Haggai", "Zechariah"],
	["Malachi", "", "", "", ""]];

document.write("Books of the Bible: Old Testament<br />");
document.write("------------------------------------------------------------------------<br />");
for (var i = 0; i < 10; ++i) {
	for (var j = 0; j < 5; ++j) {
		document.write(saaOldTestament[i][j]);
		for (var k = saaOldTestament[i][j].length; k < 15; ++k) {
			document.write(" ");
		}
	}
	document.write("<br />");
}
document.write("<br /><br />");

var saaNewTestament = new Array(7);
saaNewTestament[0] = new Array("Matthew", "Mark", "Luke", "John");
saaNewTestament[1] = new Array("Acts", "Romans", "1 Corinthians", "2 Corinthians");
saaNewTestament[2] = new Array("Galatians", "Ephesians", "Philippians", "Colossians");
saaNewTestament[3] = new Array("1 Thessalonians", "2 Thessalonians", "1 Timothy", "2 Timothy");
saaNewTestament[4] = new Array("Titus", "Philemon", "Hebrews", "James");
saaNewTestament[5] = new Array("1 Peter", "2 Peter", "1 John", "2 John");
saaNewTestament[6] = new Array("3 John", "Jude", "Revelation", "");

document.write("Books of the Bible: New Testament<br />");
document.write("----------------------------------------------------------------<br />");
for (var i = 0; i < 7; ++i) {
	for (var j = 0; j < 4; ++j) {
		document.write(saaNewTestament[i][j]);
		for (var k = saaNewTestament[i][j].length; k < 17; ++k) {
			document.write(" ");
		}
	}
	document.write("<br />");
}
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.