Lesson 1
Lesson 1
to Javascript
welcome.html
11
12 <script type = "text/javascript">
(1 of 1)
13 <!--
14 document.writeln(
15 "<h1>Welcome to JavaScript Programming!</h1>" );
16 // -->
17 </script>
18
19 </head><body></body>
20 </html>
welcome2.html
11
12 <script type = "text/javascript">
13 <!--
14
15
(1 of 1)
document.write( "<h1 style = \"color: magenta\">" );
document.write( "Welcome to JavaScript " +
16 "Programming!</h1>" );
17 // -->
18 </script>
19
20 </head><body></body>
21 </html>
welcome3.html
11 <script type = "text/javascript">
12 <!--
13 document.writeln( "<h1>Welcome to<br />JavaScript" +
14
15
1 of 1 "<br />Programming!</h1>" );
// -->
16 </script>
17
18 </head><body></body>
19 </html>
welcome4.html
11 <script type = "text/javascript">
12 <!--
13 window.alert( "Welcome to\nJavaScript\nProgramming!" );
14
15
1 of 1
// -->
</script>
16
17 </head>
18
19 <body>
20 <p>Click Refresh (or Reload) to run this script again.</p>
21 </body>
22 </html>
\' Single quote. Used to represent a single quote character in a string. For
example,
window.alert( '\'in quotes\'' );
displays 'in quotes' in an alert dialog.
welcome5.html
11
12 <script type = "text/javascript">
13 <!--
14
15
(1 of 2)
var name; // string entered by the user
welcome5.html
(2 of 2)
Fig. 7.7 Prompt dialog displayed by the window object’s prompt method.
Addition.html
11
12 <script type = "text/javascript">
13 <!--
14
15
(1 of 2)
var firstNumber,
secondNumber,
// first string entered by user
// second string entered by user
16 number1, // first number to add
17 number2, // second number to add
18 sum; // sum of number1 and number2
19
20 // read in first number from user as a string
21 firstNumber =
22 window.prompt( "Enter first integer", "0" );
23
number1 45
Fig. 7.9 Memory location showing the name and value of variable number1.
number1 45
number2 72
Fig. 7.10 Memory locations after values for variables number1 and number2 have been input.
number1 45
number2 72
sum 117
Fig. 7.11 Memory locations after calculating the sum of number1 and number2.
Subtraction - p–c p - c
Multiplication * bm b * m
/ x-- x / y
Division x / y or y
or xy
Remainder % r mod s r % s
Fig. 7.12 Arithmetic operators.
2 * 5 is 10 (Leftmost multiplication)
Step 2. y = 10 * 5 + 3 * 5 + 7;
10 * 5 is 50 (Leftmost multiplication)
Step 3. y = 50 + 3 * 5 + 7;
Step 4. y = 50 + 15 + 7;
50 + 15 is 65 (Leftmost addition)
Step 5. y = 65 + 7;
65 + 7 is 72 (Last addition)
welcome6.html
11
12 <script type = "text/javascript">
13 <!--
14
15
(1 of 3)
var name, // string entered by the user
now = new Date(), // current date and time
16 hour = now.getHours(); // current hour (0-23)
17
18 // read the name from the prompt box as a string
19 name = window.prompt( "Please enter your name", "GalAnt" );
20
21 // determine whether it is morning
22 if ( hour < 12 )
23 document.write( "<h1>Good Morning, " );
24
welcome6.html
35 // determine whether it is after 6 PM
36 if ( hour >= 6 )
37 document.write( "<h1>Good Evening, " );
38
39
(2 of 3)
}
40 document.writeln( name +
41 ", welcome to JavaScript programming!</h1>" );
42 // -->
43 </script>
44
45 </head>
46
welcome6.html
(3 of 3)