COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

22
COM311 H Zheng, School of C&M, UUJ 1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Transcript of COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Page 1: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 1

Dynamic Web Authoring

JavaScript Basics

(Array and Function)

Page 2: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Teaching Plan

Feedback on week3 practicals Array Function Method

COM311 H Zheng, School of C&M, UUJ 2

Page 3: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Feedback on practicals Errors caused when copy & paste codes from word

document – try to avoid Error – imaging loading Variable name:

• “function” – reserved word How to check the results

• window.document.write ( )• Use comments

Submission: • Only submit a correct live link once, no file upload at this

stage• Feedback – in class, and lab

COM311 H Zheng, School of C&M, UUJ 3

Page 4: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 4

Array The array is a special type of

variableArrays – a collection of data Each element of an array is a variablea variable is a container to hold a

value an array is a container to hold

multiple valuesJavascript Array is an array object

Page 5: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Array example

strawberry orange apple watermelon

fruit1 fruit2 fruit3 fruit4

fruit[0] fruit[1] fruit[2] fruit[3]

COM311 H Zheng, School of C&M, UUJ 5

Array identifier: fruitelements: fruit[0], fruit[1], fruit[2],fruit[3]length of the array: 4

Page 6: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

6

Array elementfruit[ 0 ]

Identifier Square bracket

Index

Page 7: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

7

fruit = new Array( 4 )

identifier of the new instance of Array, or name of the Array

The ‘new’ operator creates an instance

This is the parent object of the new instance

Length of the new fruit Array

Pair of parentheses

‘assignment’ operator

Page 8: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Define an array

The numbering of an array always begins at 0var array_identifier = new Array();var array_identifier = new Array(length);var array_identifier = new Array

(element1, element2, element3,…elementn)

COM311 H Zheng, School of C&M, UUJ 8

Page 9: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Array examples - 1

var rack = [ ]; rack[0] = “first”; rack[1] = “Second”; or: var rack=[ “First”, “Second”]

COM311 H Zheng, School of C&M, UUJ 9

index Value of element

0 “First”

1 “Second”

Page 10: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Array examples - 2

var numberArray = [1, 2, 3, 5, 8, 13, 21, 34];

var stringArray = ["Veni", "Vidi", "Vici"]; var mixedArray = [235, "Parramatta",

"Road"];Exercise: Define 4 Ulster campus using

an Array.

COM311 H Zheng, School of C&M, UUJ 10

Page 11: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Array of arrays var subArray1 = ["Paris", "Lyon", "Nice"];

var subArray2 = ["Amsterdam", "Eindhoven", "Utrecht"];

var subArray3 = ["Madrid", "Barcelona", "Seville"];

var superArray = [subArray1, subArray2, subArray3];

var city = superArray[0][2]; // get the third [2] element of the first [0] array, “Nice”

COM311 H Zheng, School of C&M, UUJ 11

Page 12: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Array property

length var myArray = [“first”,”second”,”third”]; var total = myArray.length; alert (total); //display in an pop-up alert

window

COM311 H Zheng, School of C&M, UUJ 12

Page 13: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 13

Function

Function: a set of statements for a designed task

Define a function:

function function_name(parameter1, parameter2,…)

{ JavaScript Statements

}

List of arguments taken, can be empty, when called, need to have same length and order

Valid identifier name

Page 14: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Functions (contd.)

• Defining functions–All variables declared in function are called

local• Do not exist outside current function

–Parameters• Also local variables

–Promotes reusability• Keep short

• Name clearly

COM311 H Zheng, School of C&M, UUJ 14

Page 15: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Functions (contd.)

Returning control - return statement, syntax:

return expression;examples:

return true;return false;return (x+y);return;

COM311 H Zheng, School of C&M, UUJ 15

Page 16: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 16

Function example:

function doSomething( ){var theVisitor = document.myform.visitor.value;Window.alert(“Is this OK,” + theVisitor + “?”);

}

function twoAdd(a, b){var theResult = a+b;return theResult;

}Q: how to call the function?

Page 17: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 17

Method

Methods - Actions that are performed with or to an object• can be used to:

• Open a new browser window

• Write text to the current XHTML document

• Navigate the browser history

• Display a dialog box

Syntax:

object name . method name (parameters)

Page 18: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 18

Method (contd.)

Two useful output methods:

• document.write(“Greetings JavaScript Students”);

• window.alert (“You got 5 marks!); //BOMNote: you can use // to add comments

behind a statement, or /* and */ to comment out a block of Javascript statments

Page 19: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

Listing 1.3 <html><head><title> A Basic Function </title> <script type="text/javascript" language="JavaScript"> <!--Hides scripts from really old browsers. function doSomething(){ var theVisitor = document.myform.visitor.value; window.alert("Is this OK, " + theVisitor + "?"); //document.write("Is this OK, " + theVisitor + "?"); } //ends script hiding --> </script> </head> <body bgcolor="white"> <p> Please type your name and click the button. </p> <form name="myform"> <input type="text" size="30" name="visitor”> <br/ > <input type="button" name="mybutton" value="Do Something"

onclick="doSomething();"> </form> </body></html>

COM311 H Zheng, School of C&M, UUJ 19

functions must be placed in head section!!

Page 20: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 20

Debugging JavaScript

Basic Debugging Techniques

Page 21: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 21

Logic and Debugging

Debugging• Act of tracing and resolving errors in a program

Three types of errors• Syntax

• Invalid or incorrect statements• Missing symbols (e.g., curly brace)• Incorrect spelling or mistypes• Using different case when referencing variables

• Run-time• Call to function that hasn’t been defined• Referencing a variable that hasn’t been declared• Division by zero

• Logic• Performing one operation when another is intended

• e.g., multiplying instead of dividing

• Infinite loops

Page 22: COM311H Zheng, School of C&M, UUJ1 Dynamic Web Authoring JavaScript Basics (Array and Function)

COM311 H Zheng, School of C&M, UUJ 22

Debugging – show example in class In Firefox: Firefox produces the most sensible and helpful messages

of current browsers

• Tools => Web Developer=> Error Console In IE versions higher than 4:

• Must turn on error notification

• Tools menu => Internet Options => Advanced

• In Browsing category:

• Select “Display a notification about every script error” In Safari, if you have web developer tool, you can use Error console

too. In Opera: Tools => advanced => Error Console In Chrome, view => developer => Javascript console Messages don’t always identify the actual problem

• Don’t assume that the problem is only with the line number shown in the error message

Tracing Errors with the alert() or write() Methods