mstccu'16 - aspiration webbers - session 4 - intro. to js

41
Aspiration Webbers Wo rkshop Session # 4 Prepared & Pres ented By: Moataz Hesham Gamal El-Din MSTC’16

Upload: moatazhesham

Post on 23-Jan-2017

195 views

Category:

Education


2 download

TRANSCRIPT

Page 1: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Aspiration Webbers

Workshop

Session # 4

Prepared & Presented By:

Moataz Hesham Gamal El-Din

MSTC’16

Page 2: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Introduction to JavaScript

Page 3: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Agenda• Revision

• JavaScript Syntax• Let’s try

• JavaScript Display Possibilities

Page 4: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Revision

Page 5: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Revision• HTML , CSS and JavaScript

What is

When to use JavaScript ? Why JavaScript ?

Where to Write JavaScript ?

?

• JavaScript:

Page 6: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Where to write JavaScript ?

Page 7: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Where to …… ? • HTML file : - i.e. <script > window. alert(“Hello JS");

</script>

• External:- i.e. file.js<script type="text/JavaScript" src=“file.js"> </script>

Page 8: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript Display Possibilities

Page 9: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript Display Possibilities

• Alert box : • Window.alert() ;

• HTML output: • document.write() ;

• HTML Element : • doucment.getElementById(“”).innerHTML = value ;

• Browser console : • Console.log();

Page 10: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript Syntax

Page 11: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Variables

<script>

var variable_name ;//ORvariable_name ;

</script>

Page 12: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Data Types• String :

<script>var name = ”Moataz” ;

</script>• number:

<script>var salary= 1250;

</script>• boolean:

<script>var discount= true;var discount= false;

</script>

Page 13: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Data Types• array:

<script>var cars= [ ”BMW”, ”Mercedes” ,

”Fiat” ];</script>• object: <script>

var person = { firstname : “Moataz“, lastname : “Hesham",

age : 21};

</script>

Page 14: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Conditions

Page 15: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript If...Else Statements• The if Statement :

if (condition) {    block of code to be executed if the

condition is true}

• Example:if (weather < 5){

document.write(“the weather is cold”);}

Page 16: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript If...Else Statements• The else Statement :

if (weather < 5){document.write(“the weather is

cold”);} else {

document.write(“the weather is fine”);}

Page 17: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript If...Else Statements• The else if Statement :

if (weather < 20){document.write(“the weather is

cold”);} else if (weather >30 ){

document.write(“the weather is hot”);}else {

document.write(“the weather is fine”);}

Page 18: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

JavaScript If...Else Statements• General rule :

if (condition1) {    block of code to be executed if

condition1 is true} else if (condition2) {

    block of code to be executed if the condition1 is false and condition2 is true

} else {    block of code to be executed if the

condition1 is false and condition2 is false}

Page 19: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Operators

Page 20: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Arithmetic Operators

Page 21: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Logical Operators

Page 22: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Comparison Operators• Assume X = 10

Page 23: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Loops

Page 24: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Loops

Page 25: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Loops

• Different Ways :

1-for loop

2-while 3-do while

4-for/in loop

Page 26: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

for loop

For syntax : for ( start ; condition ; change start value ){

// code}

Page 27: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

for loopExample :

Page 28: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

while loop

while syntax : while (condition){

// code}

Page 29: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

while loopExample :

Page 30: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Functions

Page 31: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Functions

Page 32: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

functions

• Function syntax :

function sum ( x , y ){ var result = x + y ;

document.write(result) ; }

Code Block

Function name

parameters

Page 33: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

functions

• Function syntax :

function sum ( x , y ){ var result = x + y ;

return result ; }

Code Block

Function name

parameters

Page 34: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

How to use functions

Answer :

1 - Call function.

2 - User event.

Page 35: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Call functions

Page 36: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Call functions

Page 37: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

User event

Page 38: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

LET’s Try !

Page 39: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS
Page 40: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

Any Questions ?

Page 41: MSTCCU'16 - Aspiration Webbers - Session 4 - intro. to JS

/moataz.hesham.5

Contact Me:

[email protected]