Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 58 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
58
Dung lượng
1,22 MB
Nội dung
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program. 2004 (3rd) edition. By Deitel, Deitel, and Goldberg. Published by Prentice Hall. ISBN 0-13-145091-3 2 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3 Programmer-Dened Functions 10.4 Function Denitions 10.5 Random-Number Generation 10.6 Example: Game of Chance 10.7 Another Example: Random Image Generator 10.8 Scope Rules 10.9 JavaScript Global Functions 10.10 Recursion 10.11 Recursion vs. Iteration 10.12 Web Resources 3 Objectives • In this tutorial, you will learn: – To understand how to construct programs modularly from small pieces called functions. – To be able to create new functions. – To understand the mechanisms used to pass information between functions. – To introduce simulation techniques that use random-number generation. – To understand how the visibility of identifiers is limited to specific regions of programs. 4 10.2 Program Modules in JavaScript • Modules in JavaScript – Functions – Methods • Belong to an object – JavaScript includes many useful pre- defined methods • Combine with programmer-defined methods to make a program 5 10.2 Program Modules in JavaScript • Function calls – Name – Left parenthesis – Arguments separated by commas • Constants, variables or expressions – Right parenthesis – Examples: total += parseFloat( inputValue ); total += parseFloat( s1 + s2 ); 6 10.3 Programmer-Defined Functions • 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 7 10.4 Function Definitions • Format of a function definition function function-name( parameter-list ) { declarations and statements } – Function name any valid identifier – Parameter list names of variables that will receive arguments • Must have same number as function call • May be empty – Declarations and statements • Function body (“block” of code) 8 10.4 Function Definitions • Returning control – return statement – Can return either nothing, or a value return expression; – No return statement same as return; – Not returning a value when expected is an error 9 10.4 Function Definitions • Writing a function to square two numbers – for loop from 1 to 10 – Pass each number as argument to square – return value of argument multiplied by itself – Display result 10 1 <?xml version = "1.0"?> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 <! Fig. 10.2: SquareInt.html > 6 <! Square function > 7 8 <html xmlns = "http://www.w3.org/1999/xhtml"> 9 <head> 10 <title>A Programmer-Defined square Function</title> 11 12 <script type = "text/javascript"> 13 <! 14 document.writeln( 15 "<h1>Square the numbers from 1 to 10</h1>" ); 16 17 // square the numbers from 1 to 10 18 for ( var x = 1; x <= 10; ++x ) 19 document.writeln( "The square of " + x + " is " + 20 square( x ) + "<br />" ); 21 Calling function square and passing it the value of x. Variable y gets the value of variable x. The return statement passes the value of y * y back to the calling function. [...]... Using Arrays Random Image Generator Using Arrays References and Reference Parameters Passing Arrays to Functions Sorting Arrays Searching Arrays: Linear Search and Binary Search Multidimensional Arrays Building an Online Quiz Web Resources 24 Objectives • In this tutorial, you will learn: – To introduce the array data structure – To understand the use of arrays to store, sort and search lists and tables... values – To understand how to declare an array, initialize an array and refer to individual elements of an array – To be able to pass arrays to functions – To be able to search and sort an array – To be able to declare and manipulate multidimensional arrays 25 11.1 Introduction • Arrays – Data structures of related items • Also called Collections – Dynamic 26 11.2 Arrays • Arrays in JavaScript – Each... base case, functions return 22 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program 2004 (3rd) edition By Deitel, Deitel, and Goldberg Published by Prentice Hall ISBN 0-13-145091-3 23 Chapter 11 - JavaScript: Arrays Outline 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 11.9 11.10 11.11 11.12 Introduction Arrays Declaring and Allocating Arrays Examples... 42 Function functionB multiplies the value of x by 10 18 10.9 JavaScript Global Functions • Global object – Always available – Provides 7 methods – Do not need to explicitly reference Global before method call – Also holds all global variables, user defined functions 19 10.9 JavaScript Global Functions Global function escape eval isFinite isNaN Description This function takes a string argument and. .. 31 function functionA() 32 { 33 var x = 25; 34 Function start changes the value of x to 5 // initialized each time // functionA is called 35 36 document.writeln( "local x in functionA is " + 37 x + " after entering functionA" ); 38 The value of x is incremented ++x; 39 Function functionA changes the value of x to 25 document.writeln( "local x in functionA is " + 40 41 x + " before exiting functionA"... Fig 10.9 JavaScript global functions 20 10.9 JavaScript Global Functions Global function parseFloat parseInt unescape Description This function takes a string argument and attempts to convert the beginning of the string into a floatingpoint value If the conversion is unsuccessful, the function returns NaN; otherwise, it returns the converted value (e.g., parseFloat( "abc123.45" ) returns NaN, and parseFloat(... element • Name of array • Brackets • Number of element – Arrays know their length • length property 27 11.2 Arrays 6 0 c[ 3 ] 72 c[ 4 ] 1543 c[ 5 ] -89 c[ 6 ] 0 c[ 7 ] 62 c[ 8 ] Fig 11.1 -45 c[ 2 ] Position number (index or subscript) of the element within array c c[ 0 ] c[ 1 ] Name of array -3 c[ 9 ] 1 c[ 10 ] 6453 c[ 11 ] 78 A 12-element array 28 11.3 Declaring and Allocating Arrays • Arrays in memory... "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4 5 Array > > 7 8 9 10 Sum the Elements of an Array 11 12 SumArray.html (1 of 2) 13 function start() 15 { The for loop sums the values contained in the 10element integer array called theArray 16 var theArray = [ 1, 2, 3, 4, 5, 6,...• 10.5 Random-Number Generation Random-number generation introduces element of chance – Math.random var randomValue = Math.random(); – Floating point value between 0 and 1 – Adjust range by scaling and shifting – Math.floor • Always round down Math.floor( 1 + Math.random() * 6 ) 11 1 2 . reusability • Keep short • Name clearly 7 10.4 Function Definitions • Format of a function definition function function-name( parameter-list ) { declarations and statements } – Function name any valid identifier – Parameter. Generation • Random-number generation introduces element of chance – Math.random var randomValue = Math.random(); – Floating point value between 0 and 1 – Adjust range by scaling and shifting – Math.floor • Always. understand how to construct programs modularly from small pieces called functions. – To be able to create new functions. – To understand the mechanisms used to pass information between functions. – To