www.crosswinds.net/~fishcaro/day_J8_online_tutorial.htm
<SCRIPT LANGUAGE="JavaScript">
x = "outside_function";
document.write("Outside the function, x = ",x,"<BR>");
function test() {
x = "inside_function";
document.write("Inside the function, x = ",x,"<BR>");
}
test();
document.write("After the function, x = ",x,"<BR>");
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
x = "outside_function";
document.write("Outside the function, x = ",x,"<BR>");
function test() {
var x = "inside_function";
document.write("Inside the function, x = ",x,"<BR>");
}
test();
document.write("After the function, x = ",x,"<BR>");
</SCRIPT>
For each
operator, be able to give an example that illustrates the use
of the operator.
For example: Arithmetic modulus operator: 10%8 gives the result 0 (why?)
Note: the string operator '+' is called the concatenation operator. To concatenate means to connect in a series or chain.
The increment and decrement operators are frequently used in FOR loops: type this code and study what happens.
<SCRIPT LANGUAGE="JavaScript">
for (x=1; x<=10; x++) {
document.write(x," "); }
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
for (x=10; x>=1; x--) {
document.write(x," "); }
</SCRIPT>
In general, the pattern x blah= y is a shortcut for x = x blah y .
Thus:
x += y is a shortcut for x = x + y .
x -= y is a shortcut for x = x - y .
x *= y is a shortcut for x = x * y .
x /= y is a shortcut for x = x / y .
x %= y is a shortcut for x = x % y .
Be able to give THREE DIFFERENT ways to take a variable x, add 1 to it, and then store the new value back in x.
Be able to give shortcuts for each of the following operations:
x = x+3;
x = x/3;
x = x*3;
x = x-3;
x = x%3;
A UNARY operator is one that requires only one number. For example, ++ is a unary operator, because a single number is incremented.
Be able to identify each JavaScript operator as either BINARY or UNARY.
Write a function that creates a button that says "My function". When the button is clicked, your function is called. Your function should bring up an alert message that says "Have a nice day!"
SO... get out the Simpletext and your favorite browser! Practice writing this code, WITHOUT LOOKING AT ANYTHING! When you can do it two times in a row, from scratch, without looking, then you're ready for class quizzes and tests!
Test your function on all these lists:
5,7,9,...,275
4,7,10,...,154
-4,0,4,8,...,140
new
" operator. This
is why you see:var d = new Date();
var d = new Date();
document.write(d,"<BR>")
document.write(d.getDate(),"<BR>")
document.write(d.getDay(),"<BR>")
(The days are numbered 0 to 6; which day is 0? What number is Wednesday?)document.write(d.getFullYear(),"<BR>")
document.write(d.getHours(),"<BR>")
document.write(d.getMinutes(),"<BR>")
document.write(d.getSeconds(),"<BR>")
document.write(d.getMonth(),"<BR>")
(condition) ? value1: value2;
ret = (x==5) ? "five": "not five"
document.write( (x==5) ? "five": "not five" );
x = 5;
document.write( (x==5) ? "five ": "not five " );
document.write( (x==4) ? "four ": "not four " );
The basic if/else statement is useful for executing TWO pieces of code: one if a statement is true,
the other if a statement is false. But when you want to do LOTS of different things, use
the structure that is illustrated in the following example:
if (n<0) {
// When n is less than zero, do this stuff.
}
else if (n>=0 && n<2) {
// When n is between 0 and 2, do this stuff.
}
else if (n>=2 && n<3) {
// When n is between 2 and 3, do this stuff. And on and on and on.
}
Now, re-visit problem #12 (and, a few little twists are being added). Use the "if... else if" structure
to do the following: randomly choose a number between 0
and 1, and print it out. If the number is between 0 and 0.4, say "between 0 and 0.4". If
it's beween 0.4 and 0.7, say "between 0.4 and 0.7". If it's greater than 0.7, say "greater
than 0.7". Then, ask the user if they want to do it again (and proceed accordingly).
k=1; // initialization
while (k<=5) { // the code block will be executed while k is less than or equal to 5
document.write(k,"<BR>");
k++; // increment k
}
i=5;
while (i<5) {
document.write(i,"<BR>"); i--;
}
i=5;
do {
document.write(i,"<BR>"); i--;
}
while (i<5);
i=1; //
while (i<=5) {
document.write(i,"<BR>");
i++;
}
for (i=1; i<=5; i++) {
document.write(i,"<BR>");
}
Which is more compact? Mostly, it is just personal preference
that dictates whether someone codes with "while" or "for".string = "Carol";
document.write(string.fontsize(3));
x = 5;
x=5;
x= 5;