Every web page is a document object.
Tags/elements are referred to as nodes, they all have an ID name. A block of text can also be considered a node.
JavaScript
Tuesday, August 6, 2013
Saturday, July 27, 2013
Date Objects
Do you ever shut up!
<script>
function saySomething() {
document.write("Blah ");
}
setInterval("saySomething()", 500);//1000 mm = 1 sec
function printTime() {
var now = new Date();// Date is a pre-built object
//This gets the current date/time and stores it in the now variable
var month = now.getMonth();//method to get hour from date object
var day = now.getDate();
var year = now.getFullYear();
var hour = now.getHours();//method to get hour from date object
var minute = now.getMinutes();
var sec = now.getSeconds();
document.write(month + "/" + day + "/" + year + "--" + hour + ":" + minute + ":" + sec + "<br>");
}
setInterval("printTime()", 1000);
</script>
Friday, July 26, 2013
Math
Some Properties of the Math Object
<script>document.write("Euler's constant - " + Math.E + "<br>Mmmm Pi - " + Math.PI + "<br>");
</script>
"Hey there you little sqrt."
<script>var n = prompt("Enter a Number", "");
var answer = Math.sqrt(n);
alert("The square root of " + n + " is " + answer);
</script>
Dynamic Loop Condition
<script>
var favoriteAnimals = new Array("Brown Chicken", "Brown cow", "Green Tortoise", "White Rabbit", "Pink Panther");
favoriteAnimals.sort();
for (i = 0; i < favoriteAnimals.length; i ++) {
document.write(favoriteAnimals[i] + "<br>");
}
</script>
Now if the number of the items in the favoriteAnimals Array changes so will the conditional of the forloop.
var favoriteAnimals = new Array("Brown Chicken", "Brown cow", "Green Tortoise", "White Rabbit", "Pink Panther");
favoriteAnimals.sort();
for (i = 0; i < favoriteAnimals.length; i ++) {
document.write(favoriteAnimals[i] + "<br>");
}
</script>
Now if the number of the items in the favoriteAnimals Array changes so will the conditional of the forloop.
User Defined Variables
♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦
You have such
a beautiful
Movie Star Name
<script>
var middleName = prompt("Movie Star Name:", "What is your middle name");
var streetName = prompt("Movie Star Name:", "What street do you live on");
//you probably want a vacant text-field, in which case you can use an empty set of quotes
document.write("Your movie star name is " + middleName + " " + streetName + "!");
</script>
♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦♦
Use the Array index as the counter to let
the user add items to the Array
<script>
var userDefinedArray = new Array(10);
for (i = 1; i <= 10; i++) {
userDefinedArray[i] = prompt("What is your number " + i + " favorite animal", "");
}
document.write("Your ten most favorite animals are" + userDefinedArray.join(", "));
</script>
Array Methods
<script>
var design = new Array("Typograhpy", "Photoshop", "Illustator");
var development = new Array("HTML5", "CSS3", "PHP", "MySQL", "JavaScript");
var programming = new Array(".NET", "MS SQL", "Java");
var courses = design.concat(development, programming);
document.write(courses[10]);
</script>
Join and Pop
<script>var design = new Array("Typograhpy", "Photoshop", "Illustator");
var development = new Array("HTML5", "CSS3", "PHP", "MySQL", "JavaScript");
var programming = new Array(".NET", "MS SQL", "Java", "undefined");
//3 arrays
programming.pop();//This will remove the last index from the 3rd Array
document.write(programming[4] + "<br>");//I am undefined
//Now add both the 2nd and 3rd Arrays to the 1st Array and store in a new Array
var courses = design.concat(development, programming);
//////////////////////////////////////////////////////////////////////////////////////////////////////
var join1 = courses.join(", ");//join, and add comma - space between indices
document.write(join1 + "<br>");
//////////////////////////////////////////////////////////////////////////////////////////////////////
</script>
Instead of writing these last two lines, you can simply write:
document.write(courses.join(", "));
//////////////////////////////////////////////////////////
This next method should be called shove, because it shoves new elements into your Array:
courses.push("Flash", "jQuery");
List alphabetically:
courses.sort();
Reverse it first, then join echo:
courses.reverse();
document.write(courses.join(", "));
Arrays
<script>
var beatles = new Array("johny", "george", "ringo", "paul", "paul2");
document.write(beatles[3] + " is dead");//access the array index
</script>
##############################################################
var beatles = new Array(3);
beatles[0] = "john";
beatles[1] = "george";
beatles[2] = "ringo";
beatles[3] = "paul";
beatles[4] = "paul2";
document.write(beatles[3] + " is dead");
</script>
//You can't always enter all your information into your array as soon as you create it, and you might not know how many items the array will include,
var myClasses = new Array();
myClasses[0] = "Web 110: A+";
myClasses[1] = "Web 120: A+";
myClasses[2] = "ITC 280: A+";
myClasses[3] = "Web 150: A+";
myClasses[4] = "Math 85: F-";
myClasses[5] = "Phil 106: A-";
//Arrays are objects and have Properties and Methods.
document.write(myClasses.length);
</script>
An Associative Array uses strings instead of numbers as an index
An Associative Array uses strings instead of numbers as an index
<script>
var weather = new Array();
weather["seattle"] = "party cloudy";
weather["portland"] = "chance of rain";
weather["los angeles"] = "Who Cares";
document.write("The weather will be " + weather["portland"] + " today in Portland.");
</script>
Whatever
var weather = new Array();
weather["seattle"] = "party cloudy";
weather["portland"] = "chance of rain";
weather["los angeles"] = "Who Cares";
document.write("The weather will be " + weather["portland"] + " today in Portland.");
</script>
Whatever
Subscribe to:
Comments (Atom)
