Docsity

MATERIAL LEARNING AND MATERIAL FOR PROGRAMMING JAVA, C#, C, HTML, CSS, Study Guides, Projects, Research of Programming Languages

Partial preview of the text

Download MATERIAL LEARNING AND MATERIAL FOR PROGRAMMING JAVA, C#, C, HTML, CSS and more Study Guides, Projects, Research Programming Languages in PDF only on Docsity!

WEEK 5-

INTRODUCTION TO

JAVASCRIPT

WS

USING JAVASCRIPT

Learning Outcomes: At the end of the session, the students should be able to:

  1. Create a functional website using JavaScript JAVASCRIPT 2

4 Variables

JavaScript Variable Naming Conventions

Examples:

Types of JavaScript Variables:

  1. Global Variables
  2. Local Variables Data Types Possible Variable Data Types:
  • (^) Alphabetical
  • (^) Integer, Floating-point
  • (^) Boolean/Logical
  • (^) Strings
  • (^) Null value Data Type Conversion Functions:
  • (^) parseInt()
  • (^) parseFloat() JAVASCRIPT

5 Strings What are strings? Example: strTest = “Welcome to QCU”; The new Keyword Example: strTest = new String(“Welcome to QCU”); String Concatenation Example 1:

Example 2: strTest += “.”; JAVASCRIPT

7 Expressions and Operators Examples:

  • (^) message = “This is” + “a test”;
  • (^) result = 5 + 7;
  • (^) score = score – 1;
  • (^) total = quantity * price;
  • (^) average = sum / 4;
  • (^) Remainder = sum % 4;
  • (^) tries ++;
  • (^) Total - -; JAVASCRIPT

8 Arrays What are arrays? Special variables which can hold more than one value at a time and is treated as a single unit Creating Arrays Example 1: var score = new Array(); score [0] = 39; score [1] = 40; score [2] = 100; score [3] = 49; JAVASCRIPT

10 Conditional Statements

The If Statement

Example:

var x = 10; If (x == 10) window.alert(“The number is 10!”);

Conditional Expressions

Examples:

(x == 10) (y < z) Looping Structures Types of Loops

  1. For Loop Example:

for(i = 0;i < 10; i++){

document.write(“this is

line “+ i +

“
”);

Variable w/ Initial Value Condition Counter Increment/ Decrement JAVASCRIPT

11

  1. While Loop Example:

while(total < 10){

ctr++;

total += values [ctr]

  1. Do-While Loop Example:

do {

n++;

total += values [n]

while(total < 10);

JAVASCRIPT

13

Creating Objects

Example: var person = new Object(); person.firstname="John"; person.lastname="Doe"; person.age=26; person.eyecolor="brown";

Method creates an object called

person containing the properties

firstname, lastname, age, and

eyecolor.

Accessing Object Properties Example:

var x = person.age;

Code will allow variable x to hold the

value of age from object person,

which is 26.

String Objects Example:

var string1 = new

Object();

string1.prop = "I am a

property of string

object";

alert (string1.prop);

Method creates new object

string1 with string type

property prop.

Built-In Objects JAVASCRIPT

14 Regular Expressions

Syntax:

var regexPatt = new RegExp(pattern, modifiers); or simply: var regexPatt = /pattern/modifiers;

Regular Expression Modifiers:

  1. Case-insensitive matching
  2. Global matching
  3. Multiline matching JAVASCRIPT

16 Pattern Matching String Methods for Regular Expressions:

  • (^) match
  • (^) search
  • (^) replace
  • (^) split Example: var str = "The rain in SPAIN stays mainly in the plain"; var res = str.match(/ain/g);

Code creates string array res with values: ain, ain,

ain from rain, mainly and plain, respectively.

JAVASCRIPT

17 The RegExp Object

Example:

var zipcode = new RegExp(“\d{5}”,”g”);

RegExp object zipcode will find all five-digit numbers in a string. Note

the double \.

RegExp Object Methods:

  • (^) exec()
  • (^) test()

RegExp Object Properties:

  • (^) source
  • (^) global
  • (^) ignoreCase
  • (^) multiline
  • (^) final JAVASCRIPT

19

  • (^) Button
  • (^) Checkbox
  • (^) Image
  • (^) Password The INPUT Tag Syntax:

Common Input Type Values:

  • Radio
  • (^) Reset
  • (^) Submit
  • (^) Text Popup Boxes Types of Popup Boxes:
  1. Alert Box Example: function alert_box() { alert("This is an alert box"); } Function alert_box() will display an alert box when called. JAVASCRIPT

20 Client-Side JavaScript Common Components:

  • (^) Window Object
  • (^) Location Objects
  • (^) Document Objects
  • (^) Element Objects
  • (^) Event Handlers (if any) JavaScript in Web Documents Common Uses:
  • (^) Animations and Visual Effects
  • (^) Table Sorting
  • (^) Hiding and Revealing Information
  • (^) Custom Forms and Messages JAVASCRIPT