Lab 7 JavaScript
Lab 7
JavaScript
Layout
Arrays part 2
Function and Object
Arithmetic
01
02
03
To embed a Javascript in a Web page, use the element:
<script type=“text/javascript”> script commands and
comments </script>
OR: <script LANGUAGE="JavaScript"></script>
Add JavaScript to HTML
Add JavaScript to HTML
• To access an external script (.js), use:
• <script src=“External.js” type=“text/javascript”>
</script>
Add JavaScript to HTML
• The syntax for a single-line comment is:
• //comment text
• The syntax of a multi-line comment is:
• /*comment text covering several lines*/
Comment
• The syntax for a single-line comment is:
• //comment text
• The syntax of a multi-line comment is:
• /*comment text covering several lines*/
Window object:
• Alert ()
• Prompt()
• Confirm
Alert()
• alert :
– window.alert ( " Welcome " ) ;
• OR
– alert ( " This is a message " ) ;
Alert()
Prompt()
• )window.prompt ( Please enter your name ”, “your na
me “ ;
• Example:
• x = prompt( “Enter your name”,” ”);
• JavaScript statements are separated by semicolons.
Prompt()
Confirm()
• confirm ("Are you sure you want to save changes ?" ) ;
• Ok button = True
• Cancel button = false
Confirm()
Confirm()
Document object()
Variables
• var name="Ahmed" ;
• var name ;
name = "Ahmed" ;
• var x,y,z;
• var x , y , z = 10 ;
• x = " Hi " ;
y = " Ahmed " ; z=x+""+y;
Operators (+,-,*,/,%):
Operators (+,-,*,/,%):
If statements
• if...else statement - use this statement to execute some code if the
condition is true and another code if the condition is false.
Switch
Loop
Functions
IndexOf:
Objects:
• String object :
var name =new String (“ ……..“);
• Number object :
var x=new Number(numer);
• Date object :
var name=new Date();
• getDate ( ) , getHours ( ) , getMinutes ( ) , getSeconds ( )
, getMonth ( )
Example:
Exercise:
Evaluation:
1. Write a JavaScript program that prompt the user to enter
four numbers through a pop-up window and then show
the largest among them.
2. Write a JavaScript program that prompt the user to enter t
he values x and y through a pop-up window. Your program s
hould compute the expression shown below and assign the r
esult to the variable z. The program should finally print the m
ean of x, y, and z.
– Note: z=x+4y
Extra:
• Write a JavaScript program prompt the user to enter in the currency
in dollars and prints the currency in riyals.
DD
HomeworkTry extra JS