+ All Categories
Home > Documents > JavaScript - WordPress.com · 1. Write a JavaScript program that prompt the user to enter four...

JavaScript - WordPress.com · 1. Write a JavaScript program that prompt the user to enter four...

Date post: 15-Mar-2020
Category:
Upload: others
View: 9 times
Download: 0 times
Share this document with a friend
29
Lab 7 JavaScript
Transcript
Page 1: JavaScript - WordPress.com · 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

Lab 7

JavaScript

Page 2: JavaScript - WordPress.com · 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

Layout

Arrays part 2

Function and Object

Arithmetic

01

02

03

Page 3: JavaScript - WordPress.com · 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

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

Page 4: JavaScript - WordPress.com · 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

Add JavaScript to HTML

• To access an external script (.js), use:

• <script src=“External.js” type=“text/javascript”>

</script>

Page 5: JavaScript - WordPress.com · 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

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*/

Page 6: JavaScript - WordPress.com · 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

Comment

• The syntax for a single-line comment is:

• //comment text

• The syntax of a multi-line comment is:

• /*comment text covering several lines*/

Page 7: JavaScript - WordPress.com · 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

Window object:

• Alert ()

• Prompt()

• Confirm

Page 8: JavaScript - WordPress.com · 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

Alert()

• alert :

– window.alert ( " Welcome " ) ;

• OR

– alert ( " This is a message " ) ;

Page 9: JavaScript - WordPress.com · 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

Alert()

Page 10: JavaScript - WordPress.com · 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

Prompt()

• )window.prompt ( Please enter your name ”, “your na

me “ ;

• Example:

• x = prompt( “Enter your name”,” ”);

• JavaScript statements are separated by semicolons.

Page 11: JavaScript - WordPress.com · 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

Prompt()

Page 12: JavaScript - WordPress.com · 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

Confirm()

• confirm ("Are you sure you want to save changes ?" ) ;

• Ok button = True

• Cancel button = false

Page 13: JavaScript - WordPress.com · 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

Confirm()

Page 14: JavaScript - WordPress.com · 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

Confirm()

Page 15: JavaScript - WordPress.com · 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

Document object()

Page 16: JavaScript - WordPress.com · 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

Variables

• var name="Ahmed" ;

• var name ;

name = "Ahmed" ;

• var x,y,z;

• var x , y , z = 10 ;

• x = " Hi " ;

y = " Ahmed " ; z=x+""+y;

Page 17: JavaScript - WordPress.com · 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

Operators (+,-,*,/,%):

Page 18: JavaScript - WordPress.com · 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

Operators (+,-,*,/,%):

Page 19: JavaScript - WordPress.com · 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

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.

Page 20: JavaScript - WordPress.com · 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

Switch

Page 21: JavaScript - WordPress.com · 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

Loop

Page 22: JavaScript - WordPress.com · 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

Functions

Page 23: JavaScript - WordPress.com · 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

IndexOf:

Page 24: JavaScript - WordPress.com · 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

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 ( )

Page 25: JavaScript - WordPress.com · 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

Example:

Page 26: JavaScript - WordPress.com · 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

Exercise:

Page 27: JavaScript - WordPress.com · 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

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

Page 28: JavaScript - WordPress.com · 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

Extra:

• Write a JavaScript program prompt the user to enter in the currency

in dollars and prints the currency in riyals.

Page 29: JavaScript - WordPress.com · 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

DD

HomeworkTry extra JS


Recommended