Thursday, March 10, 2011

Thursday 3/10/11


<script type="text/javascript">
var preInitArray = new Array("First Item", "Second Item", "Third Item");
document.write(preInitArray[0] + "
");
document.write(preInitArray[1] + "
");
document.write(preInitArray[2] + "
");
</script>


Here is another JavaScript tutorial. Use a string instead of an index number on an array!



<script type="text/javascript">
var anArray = new Array();
anArray[0] = "Fruit";
anArray["CostOfApple"] = 0.75;
document.write(anArray[0] + "
");
document.write(anArray["CostOfApple"]);
</script>


Here is another tutorial


<script type="text/javascript">
var anArray = new Array();
var itemIndex = 0;
var itemKeyword = "CostOfApple";

anArray[itemIndex] = "fruit";
anArray[itemKeyword] = 0.75;
document.write(anArray[itemIndex] + "
");
document.write(anArray[itemKeyword]);
</script>


Here is another tutorial



<script type="text/javascript">
var bannerImages = new Array();
bannerImages[0] = "banner1.png";
bannerImages[1] = "banner2.png";
bannerImages[2] = "banner3.png";
bannerImages[3] = "banner4.png";
var randomImageIndex = Math.round(Math.random()*2);
document.write("");
</script>


here is another tutorial - you can convert an array to a string using .join


<script type="text/javascript">
var arrayThree = new Array("John", "Paul", "George", "Ringo");
var lineUp = arrayThree.join(',');
alert(lineUp);
</script>


Here is another tutorial - split the string into an array


var longString = "Matthew, Mark, Luke, John";
var apostles = longString.split(',');
alert(apostles.length);


Here is a tutorial that I modified with a function to print the array



<script type = "text/javascript">
function printArray(array) {
document.write("Array: " + "

");
for (i=0; i<array.length; i++) {
document.write(array[i] + "
");
}
document.write("

");
}

var arrayToSort = new Array("Cabbage", "Lemon", "Apple", "Pear", "Banana");
printArray(arrayToSort);
var sortedArray = arrayToSort.sort();
printArray(sortedArray);
var reverseArray = sortedArray.reverse();
printArray(sortedArray);
</script>


Final tutorial for the night:


<script type = "text/javascript">
function compareValues(value1, value2) {
document.write("" + "Compare" + "");
document.write("

");
document.write(value1 + " = " + value2 + ": ");
document.write(value1 == value2);
document.write("
");
document.write(value1 + " > " + value2 + ": ");
document.write (value1 > value2);
document.write("

");
}

compareValues("Apples", "Oranges");

compareValues(18, 44);

</script>

No comments: