334 JavaScript/JScript: Functions Chapter 11. boss. worker1 worker2 worker3. Hierarchical boss function/worker function relationship.
|
|
- Reginald Montgomery
- 5 years ago
- Views:
Transcription
1 . iw3htp_11.fm Page 334 Thursday, April 13, :33 PM 334 JavaScript/JScript: Functions Chapter JavaScript/JScript: Functions boss worker1 worker2 worker3 worker4 worker5 Fig Hierarchical boss function/worker function relationship.
2 iw3htp_11.fm Page 335 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.2: SquareInt.html --> 4 5 <HEAD> 6 <TITLE>A Programmer-Defined square Function</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 document.writeln( 10 "<H1>Square the numbers from 1 to 10</H1>" ); // square the numbers from 1 to for ( var x = 1; x <= 10; ++x ) 14 document.writeln( "The square of " + x + " is " + 15 square( x ) + "<BR>" ); // The following square function's body is executed only 18 // when the function is explicitly called // square function definition 21 function square( y ) 22 { 23 return y * y; 24 } 25 </SCRIPT> </HEAD><BODY></BODY> 28 </HTML> Fig Using programmer-defined function square.
3 iw3htp_11.fm Page 336 Thursday, April 13, :33 PM 336 JavaScript/JScript: Functions Chapter 11 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.3: maximum.html --> 4 5 <HEAD> 6 <TITLE>Finding the Maximum of Three Values</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 var input1 = window.prompt( "Enter first number", "0" ); 10 var input2 = window.prompt( "Enter second number", "0" ); 11 var input3 = window.prompt( "Enter third number", "0" ); var value1 = parsefloat( input1 ); 14 var value2 = parsefloat( input2 ); 15 var value3 = parsefloat( input3 ); var maxvalue = maximum( value1, value2, value3 ); document.writeln( "First number: " + value "<BR>Second number: " + value "<BR>Third number: " + value "<BR>Maximum is: " + maxvalue ); // maximum method definition (called from line 17) 25 function maximum( x, y, z ) 26 { 27 return Math.max( x, Math.max( y, z ) ); 28 } 29 </SCRIPT> </HEAD> 32 <BODY> 33 <P>Click Refresh (or Reload) to run the script again</p> 34 </BODY> 35 </HTML> Fig Programmer-defined maximum function (part 1 of 2).
4 iw3htp_11.fm Page 337 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 337 Fig Programmer-defined maximum function (part 2 of 2).
5 iw3htp_11.fm Page 338 Thursday, April 13, :33 PM 338 JavaScript/JScript: Functions Chapter 11 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.4: RandomInt.java --> 4 5 <HEAD> 6 <TITLE>Shifted and Scaled Random Integers</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 var value; document.writeln( "<H1>Random Numbers</H1>" + 12 "<TABLE BORDER = '1' WIDTH = '50%'><TR>" ); for ( var i = 1; i <= 20; i++ ) { 15 value = Math.floor( 1 + Math.random() * 6 ); 16 document.writeln( "<TD>" + value + "</TD>" ); if ( i % 5 == 0 && i!= 20 ) 19 document.writeln( "</TR><TR>" ); 20 } document.writeln( "</TR></TABLE>" ); 23 </SCRIPT> </HEAD> 26 <BODY> 27 <P>Click Refresh (or Reload) to run the script again</p> 28 </BODY> 29 </HTML> Fig Shifted and scaled random integers (part 1 of 2).
6 iw3htp_11.fm Page 339 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 339 Fig Shifted and scaled random integers (part 2 of 2).
7 iw3htp_11.fm Page 340 Thursday, April 13, :33 PM 340 JavaScript/JScript: Functions Chapter 11 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.5: RollDie.html --> 4 5 <HEAD> 6 <TITLE>Roll a Six-Sided Die 6000 Times</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 var frequency1 = 0, frequency2 = 0, 10 frequency3 = 0, frequency4 = 0, 11 frequency5 = 0, frequency6 = 0, face; // summarize results 14 for ( var roll = 1; roll <= 6000; ++roll ) { 15 face = Math.floor( 1 + Math.random() * 6 ); switch ( face ) { 18 case 1: 19 ++frequency1; 20 break; 21 case 2: 22 ++frequency2; 23 break; 24 case 3: 25 ++frequency3; 26 break; 27 case 4: 28 ++frequency4; 29 break; 30 case 5: 31 ++frequency5; 32 break; 33 case 6: 34 ++frequency6; 35 break; 36 } 37 } document.writeln( "<TABLE BORDER = '1' WIDTH = '50%'>" ); 40 document.writeln( "<TR><TD><B>Face</B></TD>" + 41 "<TD><B>Frequency</B></TD></TR>" ); 42 document.writeln( "<TR><TD>1</TD><TD>" + frequency "</TD></TR>" ); 44 document.writeln( "<TR><TD>2</TD><TD>" + frequency "</TD></TR>" ); 46 document.writeln( "<TR><TD>3</TD><TD>" + frequency "</TD></TR>" ); 48 document.writeln( "<TR><TD>4</TD><TD>" + frequency "</TD></TR>" ); 50 document.writeln( "<TR><TD>5</TD><TD>" + frequency5 + Fig Rolling a six-sided die 6000 times (part 1 of 2).
8 iw3htp_11.fm Page 341 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions "</TD></TR>" ); 52 document.writeln( "<TR><TD>6</TD><TD>" + frequency "</TD></TR></TABLE>" ); 54 </SCRIPT> </HEAD> 57 <BODY> 58 <P>Click Refresh (or Reload) to run the script again</p> 59 </BODY> 60 </HTML> Fig Rolling a six-sided die 6000 times (part 2 of 2).
9 iw3htp_11.fm Page 342 Thursday, April 13, :33 PM 342 JavaScript/JScript: Functions Chapter 11 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.6: Craps.html --> 4 5 <HEAD> 6 <TITLE>Program that Simulates the Game of Craps</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 // variables used to test the state of the game 10 var WON = 0, LOST = 1, CONTINUE_ROLLING = 2; // other variables used in program 13 var firstroll = true, // true if first roll 14 sumofdice = 0, // sum of the dice 15 mypoint = 0, // point if no win/loss on first roll 16 gamestatus = CONTINUE_ROLLING; // game not over yet // process one roll of the dice 19 function play() 20 { 21 if ( firstroll ) { // first roll of the dice 22 sumofdice = rolldice(); switch ( sumofdice ) { 25 case 7: case 11: // win on first roll 26 gamestatus = WON; 27 craps.point.value = ""; // clear point field 28 break; 29 case 2: case 3: case 12: // lose on first roll 30 gamestatus = LOST; 31 craps.point.value = ""; // clear point field 32 break; 33 default: // remember point 34 gamestatus = CONTINUE_ROLLING; 35 mypoint = sumofdice; 36 craps.point.value = mypoint; 37 firstroll = false; 38 } 39 } 40 else { 41 sumofdice = rolldice(); if ( sumofdice == mypoint ) // win by making point 44 gamestatus = WON; 45 else 46 if ( sumofdice == 7 ) // lose by rolling 7 47 gamestatus = LOST; 48 } if ( gamestatus == CONTINUE_ROLLING ) 51 window.status = "Roll again"; 52 else { 53 if ( gamestatus == WON ) Fig Program to simulate the game of craps (part 1 of 5).
10 iw3htp_11.fm Page 343 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions window.status = "Player wins. " + 55 "Click Roll Dice to play again."; 56 else 57 window.status = "Player loses. " + 58 "Click Roll Dice to play again."; firstroll = true; 61 } 62 } // roll the dice 65 function rolldice() 66 { 67 var die1, die2, worksum; die1 = Math.floor( 1 + Math.random() * 6 ); 70 die2 = Math.floor( 1 + Math.random() * 6 ); 71 worksum = die1 + die2; craps.firstdie.value = die1; 74 craps.seconddie.value = die2; 75 craps.sum.value = worksum; return worksum; 78 } 79 </SCRIPT> </HEAD> 82 <BODY> 83 <FORM NAME = "craps"> 84 <TABLE BORDER = "1"> 85 <TR><TD>Die 1</TD> 86 <TD><INPUT NAME = "firstdie" TYPE = "text"></td></tr> 87 <TR><TD>Die 2</TD> 88 <TD><INPUT NAME = "seconddie" TYPE = "text"></td></tr> 89 <TR><TD>Sum</TD> 90 <TD><INPUT NAME = "sum" TYPE = "text"></td></tr> 91 <TR><TD>Point</TD> 92 <TD><INPUT NAME = "point" TYPE = "text"></td></tr> 93 <TR><TD><INPUT TYPE = "button" VALUE = "Roll Dice" 94 ONCLICK = "play()"></td></tr> 95 </TABLE> 96 </FORM> 97 </BODY> 98 </HTML> Fig Program to simulate the game of craps (part 2 of 5).
11 iw3htp_11.fm Page 344 Thursday, April 13, :33 PM 344 JavaScript/JScript: Functions Chapter 11 A text HTML GUI component A button HTML GUI component Browser s status bar Fig Program to simulate the game of craps (part 3 of 5).
12 iw3htp_11.fm Page 345 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 345 Fig Program to simulate the game of craps (part 4 of 5).
13 iw3htp_11.fm Page 346 Thursday, April 13, :33 PM 346 JavaScript/JScript: Functions Chapter 11 Fig Program to simulate the game of craps (part 5 of 5).
14 iw3htp_11.fm Page 347 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.7: scoping.html --> 4 5 <HEAD> 6 <TITLE>A Scoping Example</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 var x = 1; // global variable function start() 12 { 13 var x = 5; // variable local to function start document.writeln( "local x in start is " + x ); functiona(); // functiona has local x 18 functionb(); // functionb uses global variable x 19 functiona(); // functiona reinitializes local x 20 functionb(); // global variable x retains its value document.writeln( 23 "<P>local x in start is " + x + "</P>" ); 24 } function functiona() 27 { 28 var x = 25; // initialized each time functiona is called document.writeln( "<P>local x in functiona is " + x + 31 " after entering functiona" ); 32 ++x; 33 document.writeln( "<BR>local x in functiona is " + x + 34 " before exiting functiona</p>" ); 35 } function functionb() 38 { 39 document.writeln( "<P>global variable x is " + x + 40 " on entering functionb" ); 41 x *= 10; 42 document.writeln( "<BR>global variable x is " + x + 43 " on exiting functionb</p>" ); 44 } 45 </SCRIPT> </HEAD> 48 <BODY ONLOAD = "start()"></body> 49 </HTML> Fig A scoping example. (part 1 of 2)
15 iw3htp_11.fm Page 348 Thursday, April 13, :33 PM 348 JavaScript/JScript: Functions Chapter 11 Fig A scoping example. (part 2 of 2)
16 iw3htp_11.fm Page 349 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 349 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Final value = 120 5! 5! = 5 * 24 = 120 is returned 5 * 4! 4! = 4 * 6 = 24 is returned 4 * 3! 3! = 3 * 2 = 6 is returned 3 * 2! 2! = 2 * 1 = 2 is returned 2 * 1! 1 returned 1 a) Procession of recursive calls. b) Values returned from each recursive call. Fig Recursive evaluation of 5!.
17 iw3htp_11.fm Page 350 Thursday, April 13, :33 PM 350 JavaScript/JScript: Functions Chapter 11 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig. 11.9: FactorialTest.html --> 4 5 <HEAD> 6 <TITLE>Recursive Factorial Function</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 document.writeln( "<H1>Factorials of 1 to 10</H1>" ); 10 document.writeln( "<TABLE BORDER = '1' WIDTH = '100%'>" ); for ( var i = 0; i <= 10; i++ ) 13 document.writeln( "<TR><TD>" + i + "!</TD><TD>" + 14 factorial( i ) + "</TD></TR>" ); document.writeln( "</TABLE>" ); // Recursive definition of function factorial 19 function factorial( number ) 20 { 21 if ( number <= 1 ) // base case 22 return 1; 23 else 24 return number * factorial( number - 1 ); 25 } 26 </SCRIPT> </HEAD><BODY></BODY> 29 </HTML> Fig Calculating factorials with a recursive function (part 1 of 2).
18 iw3htp_11.fm Page 351 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 351 Fig Calculating factorials with a recursive function (part 2 of 2).
19 iw3htp_11.fm Page 352 Thursday, April 13, :33 PM 352 JavaScript/JScript: Functions Chapter 11 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <HTML> 3 <!-- Fig : FibonacciTest.html --> 4 5 <HEAD> 6 <TITLE>Recursive Fibonacci Function</TITLE> 7 8 <SCRIPT LANGUAGE = "JavaScript"> 9 // Event handler for button HTML component in myform 10 function getfibonaccivalue() 11 { 12 var value = parseint( document.myform.number.value ); 13 window.status = 14 "Calculating Fibonacci number for " + value; 15 document.myform.result.value = fibonacci( value ); 16 window.status = "Done calculating Fibonacci number"; 17 } // Recursive definition of function fibonacci 20 function fibonacci( n ) 21 { 22 if ( n == 0 n == 1 ) // base case 23 return n; 24 else 25 return fibonacci( n - 1 ) + fibonacci( n - 2 ); 26 } 27 </SCRIPT> </HEAD> <BODY> 32 <FORM NAME = "myform"> 33 <TABLE BORDER = "1"> 34 <TR><TD>Enter an integer</td> 35 <TD><INPUT NAME = "number" TYPE = "text"></td> 36 <TD><INPUT TYPE = "button" VALUE = "Calculate" 37 ONCLICK = "getfibonaccivalue()"</tr> 38 <TR><TD>Fibonacci value</td> 39 <TD><INPUT NAME = "result" TYPE = "text"></td></tr> 40 </TABLE> 41 </FORM></BODY> 42 </HTML> Fig Recursively generating Fibonacci numbers (part 1 of 3).
20 iw3htp_11.fm Page 353 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 353 Fig Recursively generating Fibonacci numbers (part 2 of 3).
21 iw3htp_11.fm Page 354 Thursday, April 13, :33 PM 354 JavaScript/JScript: Functions Chapter 11 Fig Recursively generating Fibonacci numbers (part 3 of 3).
22 iw3htp_11.fm Page 355 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 355 f( 3 ) return f( 2 ) + f( 1 ) return f( 1 ) + f( 0 ) return 1 return 1 return 0 Fig Set of recursive calls to function fibonacci.
23 iw3htp_11.fm Page 356 Thursday, April 13, :33 PM 356 JavaScript/JScript: Functions Chapter Chapter Recursion examples and exercises 11 Factorial function Fibonacci function Greatest common divisor Sum of two integers Multiply two integers Raising an integer to an integer power Towers of Hanoi Visualizing recursion 12 Sum the elements of an array Print an array Print an array backward Check if a string is a palindrome Minimum value in an array Selection sort Eight Queens Linear search Binary search Quicksort Maze traversal 13 Printing a string input at the keyboard backward Fig Summary of recursion examples and exercises in the text.
24 iw3htp_11.fm Page 357 Thursday, April 13, :33 PM Chapter 11 JavaScript/JScript: Functions 357 Global function Description escape eval isfinite isnan parsefloat parseint unescape This function takes a string argument and returns a string in which all spaces, punctuation, accent characters and any other character that is not in the ASCII character set (see Appendix C, Character Set ) are encoded in a hexadecimal format (see Appendix F, Number Systems ) that can be represented on all platforms. This function takes a string argument representing JavaScript code to execute. The JavaScript interpreter evaluates the code and executes it when the eval function is called. This function allows JavaScript code to be stored as strings and executed dynamically. This function takes a numeric argument and returns true if the value of the argument is not NaN, Number.POSITIVE_INFINITY or Number.NEGATIVE_INFINITY; otherwise the function returns false. This function takes a numeric argument and returns true if the value of the argument is not a number; otherwise the function returns false. The function is commonly used with the return value of parseint or parsefloat to determine whether the result is a proper numeric value. This function takes a string argument and attempts to convert the beginning of the string into a floating-point value. If the conversion is not successful, the function returns NaN; otherwise, it returns the converted value (e.g., parsefloat( "abc123.45" ) returns NaN and parsefloat( "123.45abc" ) returns the floating-point value This function takes a string argument and attempts to convert the beginning of the string into an integer value. If the conversion is not successful, the function returns NaN; otherwise, it returns the converted value (e.g., parseint( "abc123" ) returns NaN and parseint( "123abc" ) returns the integer value 123. This function takes an optional second argument between 2 and 36 specifying the radix (or base) of the number. For example, 2 indicates that the first argument string is in binary format, 8 indicates that the first argument string is in octal format and 16 indicates that the first argument string is in hexadecimal format. See Appendix F, Number Systems for more information on binary, octal and hexadecimal numbers. This function takes a string as its argument and returns a string in which all characters that we previously encoded with escape are decoded. Fig JavaScript global functions.
JAVASCRIPT LESSON 4: FUNCTIONS
JAVASCRIPT LESSON 4: FUNCTIONS 11.1 Introductio n Programs that solve realworld programs More complex than programs from previous chapters Best way to develop & maintain large program: Construct from small,
More informationJavaScript: Functions Pearson Education, Inc. All rights reserved.
1 9 JavaScript: Functions 2 Form ever follows function. Louis Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare Call me Ishmael. Herman
More informationChapter 8 JavaScript/JScript: Introduction to Scripting 201
iw3htp_08.fm Page 201 Thursday, April 13, 2000 12:30 PM Chapter 8 JavaScript/JScript: Introduction to Scripting 201 8 JavaScript/JScript: Introduction to Scripting 1
More informationChapter 10 JavaScript/JScript: Control Structures II 289
IW3HTP_10.fm Page 289 Thursday, April 13, 2000 12:32 PM Chapter 10 JavaScript/JScript: Control Structures II 289 10 JavaScript/JScript: Control Structures II 1
More informationLecture 6 Part a: JavaScript
Lecture 6 Part a: JavaScript G64HLL, High Level Languages http://www.cs.nott.ac.uk/~gxo/g64hll.html Dr. Gabriela Ochoa gxo@cs.nott.ac.uk Previous lecture Basics of java script The document, window objects
More informationChapter 11 - JavaScript: Arrays
Chapter - JavaScript: Arrays 1 Outline.1 Introduction.2 Arrays.3 Declaring and Allocating Arrays. Examples Using Arrays.5 References and Reference Parameters.6 Passing Arrays to Functions. Sorting Arrays.8
More informationFunctions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.
Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create
More informationC Functions. 5.2 Program Modules in C
1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking
More informationDr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)
Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly
More informationChapter 6 - Methods Prentice Hall. All rights reserved.
Chapter 6 - Methods 1 Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument Promotion 6.7 Java API Packages 6.8 Random-Number Generation
More informationFunctions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.
Functions Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 5.1 Introduction 5.3 Math Library Functions 5.4 Functions 5.5
More informationCoding in JavaScript functions
Coding in JavaScript functions A function contains code that will be executed by an event or by a call to the function. You may call a function from anywhere within a page (or even from other pages if
More informationJavascript Lesson 3: Controlled Structures ANDREY KUTASH
Javascript Lesson 3: Controlled Structures ANDREY KUTASH 10.1 Introduction Before programming a script have a Thorough understanding of problem Carefully planned approach to solve it When writing a script,
More informationC Functions Pearson Education, Inc. All rights reserved.
1 5 C Functions 2 Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare Call me Ishmael. Herman Melville
More informationCSE123. Program Design and Modular Programming Functions 1-1
CSE123 Program Design and Modular Programming Functions 1-1 5.1 Introduction A function in C is a small sub-program performs a particular task, supports the concept of modular programming design techniques.
More informationCHAPTER 5: JavaScript Basics 99
CHAPTER 5: JavaScript Basics 99 5.2 JavaScript Keywords, Variables, and Operators 5.2.1 JavaScript Keywords break case continue default delete do else export false for function if import in new null return
More informationFunctions and Recursion
Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game
More informationJavaScript: Introduction to Scripting
iw3htp2.book Page 194 Wednesday, July 18, 2001 9:01 AM 7 JavaScript: Introduction to Scripting Objectives To be able to write simple JavaScript programs. To be able to use input and output statements.
More informationFunction Call Stack and Activation Records
71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students
More informationC: How to Program. Week /Apr/16
C: How to Program Week 8 2006/Apr/16 1 Storage class specifiers 5.11 Storage Classes Storage duration how long an object exists in memory Scope where object can be referenced in program Linkage specifies
More informationChapter 3 - Functions
Chapter 3 - Functions 1 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number Generation
More informationExercise 1: Basic HTML and JavaScript
Exercise 1: Basic HTML and JavaScript Question 1: Table Create HTML markup that produces the table as shown in Figure 1. Figure 1 Question 2: Spacing Spacing can be added using CellSpacing and CellPadding
More informationChapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++
Chapter 3 - Functions 1 Chapter 3 - Functions 2 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3. Functions 3.5 Function Definitions 3.6 Function Prototypes 3. Header Files 3.8
More informationLECTURE-2. Functions review HTML Forms. Arrays Exceptions Events. CS3101: Scripting Languages: Javascript Ramana Isukapalli
LECTURE-2 Functions review HTML Forms Arrays Exceptions Events 1 JAVASCRIPT FUNCTIONS, REVIEW Syntax function (params) { // code Note: Parameters do NOT have variable type. 1. Recall: Function
More informationChapter 9 - JavaScript: Control Structures II
Chapter 9 - JavaScript: Control Structures II Outline 9.1 Introduction 9.2 Essentials of Counter-Controlled Repetition 9.3 for Repetition Structure 9. Examples Using the for Structure 9.5 switch Multiple-Selection
More informationEAS230: Programming for Engineers Lab 1 Fall 2004
Lab1: Introduction Visual C++ Objective The objective of this lab is to teach students: To work with the Microsoft Visual C++ 6.0 environment (referred to as VC++). C++ program structure and basic input
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Progra m Components in C++ 3.3 Ma th Libra ry Func tions 3.4 Func tions 3.5 Func tion De finitions 3.6 Func tion Prototypes 3.7 He a de r File s 3.8
More informationTutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations
Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations Outline 12.1 Test-Driving the Craps Game Application 12.2 Random Number Generation 12.3 Using an enum in the Craps
More informationThe first sample. What is JavaScript?
Java Script Introduction JavaScript is the most popular scripting language on the internet, and works in all major browsers, such as Internet Explorer, Firefox, Chrome, Opera, and Safari. In this lecture
More informationReview Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous
Assignment 3 Methods Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes:
More informationAssignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:
7 Arrays Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then
More informationLecture 04 FUNCTIONS AND ARRAYS
Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions
More informationJavaScript: Control Statements I Pearson Education, Inc. All rights reserved.
1 7 JavaScript: Control Statements I 2 Let s all move one place on. Lewis Carroll The wheel is come full circle. William Shakespeare How many apples fell on Newton s head before he took the hint! Robert
More informationProgram Design Phase. Algorithm Design - Mathematical. Algorithm Design - Sequence. Verify Algorithm Y = MX + B
Program Design Phase Write Program Specifications Analysis of requirements Program specifications description Describe what the goals of the program Describe appearance of input and output Algorithm Design
More informationJavaScript: Introductionto Scripting
6 Comment is free, but facts are sacred. C. P. Scott The creditor hath a better memory than the debtor. James Howell When faced with a decision, I always ask, What would be the most fun? Peggy Walker Equality,
More informationJAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in
Type-Conversion in JavaScript Description Type conversion or typecasting is one of the very important concept in JavaScript. It refers to changing an entity or variable from one datatype to another. There
More informationA.A. 2008/09. Why introduce JavaScript. G. Cecchetti Internet Software Technologies
Internet t Software Technologies JavaScript part one IMCNE A.A. 2008/09 Gabriele Cecchetti Why introduce JavaScript To add dynamicity and interactivity to HTML pages 2 What s a script It s a little interpreted
More information12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times
} Rolling a Six-Sided Die face = 1 + randomnumbers.nextint( 6 ); The argument 6 called the scaling factor represents the number of unique values that nextint should produce (0 5) This is called scaling
More informationFundamentals of Programming Session 25
Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines
More informationCSC Web Programming. Introduction to JavaScript
CSC 242 - Web Programming Introduction to JavaScript JavaScript JavaScript is a client-side scripting language the code is executed by the web browser JavaScript is an embedded language it relies on its
More information6.5 Function Prototypes and Argument Coercion
6.5 Function Prototypes and Argument Coercion 32 Function prototype Also called a function declaration Indicates to the compiler: Name of the function Type of data returned by the function Parameters the
More informationSuch JavaScript Very Wow
Such JavaScript Very Wow Lecture 9 CGS 3066 Fall 2016 October 20, 2016 JavaScript Numbers JavaScript numbers can be written with, or without decimals. Extra large or extra small numbers can be written
More informationwrite vs. writeln Prompting as Page Loads Today s Goals CSCI 2910 Client/Server-Side Programming Intermediate File vs. HTML Output
CSCI 2910 Client/Server-Side Programming Topic: JavaScript Part 2 Today s Goals Today s lecture will cover: More objects, properties, and methods of the DOM The Math object Introduction to form validation
More informationA Balanced Introduction to Computer Science, 3/E
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN 978-0-13-216675-1 Chapter 7 Functions and Randomness 1 Predefined Functions recall: in
More informationPeople = End Users & Programmers. The Web Browser Application. Program Design Phase. Algorithm Design -Mathematical Y = MX + B
The Web Browser Application People = End Users & Programmers Clients and Components Input from mouse and keyboard Controller HTTP Client FTP Client TCP/IP Network Interface HTML/XHTML CSS JavaScript Flash
More informationChapter 3 - Functions
Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number
More informationA Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University 2011 Pearson Prentice Hall ISBN 978-0-13-216675-1 Chapter 7 Functions and Randomness 1 Predefined Functions recall: in
More informationProblem 1: Textbook Questions [4 marks]
Problem 1: Textbook Questions [4 marks] Answer the following questions from Fluency with Information Technology. Chapter 3, Short Answer #8: A company that supplies connections to the Internet is called
More informationDynamism and Detection
1 Dynamism and Detection c h a p t e r ch01 Page 1 Wednesday, June 23, 1999 2:52 PM IN THIS CHAPTER Project I: Generating Platform-Specific Content Project II: Printing Copyright Information and Last-Modified
More informationECET 264 C Programming Language with Applications
ECET 264 C Programming Language with Applications Lecture 10 C Standard Library Functions Paul I. Lin Professor of Electrical & Computer Engineering Technology http://www.etcs.ipfw.edu/~lin Lecture 10
More information[CHAPTER] 1 INTRODUCTION 1
FM_TOC C7817 47493 1/28/11 9:29 AM Page iii Table of Contents [CHAPTER] 1 INTRODUCTION 1 1.1 Two Fundamental Ideas of Computer Science: Algorithms and Information Processing...2 1.1.1 Algorithms...2 1.1.2
More informationFunctions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single
Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module
More information(from Chapters 10/11 of the text)
IT350 Web and Internet Programming SlideSet #10: JavaScript Arrays and Objects (from Chapters 10/11 of the text) Everything you ever wanted to know about arrays function initializearrays() var n1 = new
More informationClient-Side Web Technologies. JavaScript Part I
Client-Side Web Technologies JavaScript Part I JavaScript First appeared in 1996 in Netscape Navigator Main purpose was to handle input validation that was currently being done server-side Now a powerful
More informationChapter 5 - Methods Prentice Hall, Inc. All rights reserved.
1 Chapter 5 - Methods 2003 Prentice Hall, Inc. All rights reserved. 2 Introduction Modules Small pieces of a problem e.g., divide and conquer Facilitate design, implementation, operation and maintenance
More informationJavaScript: Control Statements I
7 JavaScript: Control Statements I OBJECTIVES In this chapter you will learn: Basic problem-solving techniques. To develop algorithms through the process of top-down, stepwise refinement. To use the if
More informationCONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17
List of Programs xxv List of Figures xxix List of Tables xxxiii Preface to second version xxxv PART 1 Structured Programming 1 1 Getting started 3 1.1 Programming 3 1.2 Editing source code 5 Source code
More informationIT350 Web and Internet Programming Fall 2007 SlideSet #10: JavaScript Arrays, Objects, & Cookies. (from Chapter 11/12 of the text)
IT350 Web and Internet Programming Fall 2007 SlideSet #10: JavaScript Arrays, Objects, & Cookies (from Chapter 11/12 of the text) Everything you ever wanted to know about arrays function initializearrays()
More information(from Chapter 10/11 of the text)
IT350 Web and Internet Programming Fall 2008 SlideSet #9: JavaScript Arrays, Objects, & Cookies (from Chapter 10/11 of the text) Everything you ever wanted to know about arrays function initializearrays()
More informationCOMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts
COMP519 Web Programming Lecture 16: JavaScript (Part 7) Handouts Ullrich Hustadt Department of Computer Science School of Electrical Engineering, Electronics, and Computer Science University of Liverpool
More informationMethods (Deitel chapter 6)
1 Plan 2 Methods (Deitel chapter ) Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods
More informationMethods (Deitel chapter 6)
Methods (Deitel chapter 6) 1 Plan 2 Introduction Program Modules in Java Math-Class Methods Method Declarations Argument Promotion Java API Packages Random-Number Generation Scope of Declarations Methods
More informationJavaScript CS 4640 Programming Languages for Web Applications
JavaScript CS 4640 Programming Languages for Web Applications 1 How HTML, CSS, and JS Fit Together {css} javascript() Content layer The HTML gives the page structure and adds semantics Presentation
More informationTwo Approaches to Algorithms An Example (1) Iteration (2) Recursion
2. Recursion Algorithm Two Approaches to Algorithms (1) Iteration It exploits while-loop, for-loop, repeat-until etc. Classical, conventional, and general approach (2) Recursion Self-function call It exploits
More informationRecursion Introduction OBJECTIVES
1 1 We must learn to explore all the options and possibilities that confront us in a complex and rapidly changing world. James William Fulbright O thou hast damnable iteration, and art indeed able to corrupt
More informationHTML5 and CSS3 More JavaScript Page 1
HTML5 and CSS3 More JavaScript Page 1 1 HTML5 and CSS3 MORE JAVASCRIPT 3 4 6 7 9 The Math Object The Math object lets the programmer perform built-in mathematical tasks Includes several mathematical methods
More informationLab Manual. Program Design and File Structures (P): IT-219
Lab Manual Program Design and File Structures (P): IT-219 Lab Instructions Several practicals / programs? Whether an experiment contains one or several practicals /programs One practical / program Lab
More informationCOMP 110 Programming Exercise: Simulation of the Game of Craps
COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,
More information142
Scope Rules Thus, storage duration does not affect the scope of an identifier. The only identifiers with function-prototype scope are those used in the parameter list of a function prototype. As mentioned
More informationBy: JavaScript tutorial-a simple calculator
JavaScript tutorial-a simple calculator In this small sample project, you will learn to create a simple JavaScript calculator. This calculator has only one text box and sixteen buttons. The text box allows
More informationNotes from April 3 Tuesday
Notes from April 3 Tuesday With many weeks of ce108 covering higher level computing using matlab, it is time to go lower level so we could understand more of the computer terms used frequently in the computer
More informationCIS 339 Section 2. What is JavaScript
CIS 339 Section 2 Introduction to JavaScript 9/26/2001 2001 Paul Wolfgang 1 What is JavaScript An interpreted programming language with object-oriented capabilities. Shares its syntax with Java, C++, and
More informationPart III Appendices 165
Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.
More informationA340 Laboratory Session #5
A340 Laboratory Session #5 LAB GOALS Creating multiplication table using JavaScript Creating Random numbers using the Math object Using your text editor (Notepad++ / TextWrangler) create a web page similar
More informationCS1046 Lab 4. Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to:
CS1046 Lab 4 Timing: This lab should take you 85 to 130 minutes. Objectives: By the end of this lab you should be able to: Define the terms: function, calling and user-defined function and predefined function
More information840 Database: SQL, ADO and RDS Chapter 25. Department Salary Location
iw3htp_25.fm Page 840 Tuesday, May 23, 2000 6:11 AM 840 Database: SQL, ADO and RDS Chapter 25 A record Number Name 25 Database: SQL, ADO and RDS Table: Employee Department Salary Location 23603 JONES,
More information6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.
6.S189 Homework 2 http://web.mit.edu/6.s189/www/materials.html What to turn in Checkoffs 3, 4 and 5 are due by 5 PM on Monday, January 15th. Checkoff 3 is over Exercises 3.1-3.2, Checkoff 4 is over Exercises
More informationRandom number generation, Math object and methods, HTML elements in motion, setinterval and clearinterval
Lab04 - Page 1 of 6 Lab 04 Monsters in a Race Random number generation, Math object and methods, HTML elements in motion, setinterval and clearinterval Additional task: Understanding pass-by-value Introduction
More informationLast Time: Rolling a Weighted Die
Last Time: Rolling a Weighted Die import math/rand func DieRoll() int { return rand.intn(6) + 1 Multiple Rolls When we run this program 100 times, we get the same outcome! func main() int { fmt.println(dieroll())
More informationJavascript. UNIVERSITY OF MASSACHUSETTS AMHERST CMPSCI 120 Fall 2010
Lecture 14 Javascript Announcements Project #2 New website Exam#2 No. Class Date Subject and Handout(s) 17 11/4/10 Examination Review Practice Exam PDF 18 11/9/10 Search, Safety, Security Slides PDF UMass
More informationC How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.
C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features
More informationC Programming for Electronic Engineers
C Programming for Electronic Engineers Keith Jackson BSc CEng MIEE with acknowledgement to Gavin Eamshaw MEng School of Electronic, Communication and Electrical Engineering University of Plymouth MACMILLAN
More informationFunctions, Randomness and Libraries
Functions, Randomness and Libraries 1 Predefined Functions recall: in mathematics, a function is a mapping from inputs to a single output e.g., the absolute value function: -5 5, 17.3 17.3 in JavaScript,
More information448 JavaScript/JScript: Objects Chapter 13
iw3htp_13.fm Page 448 Thursday, April 13, 2000 12:35 PM 448 JavaScript/JScript: Objects Chapter 13 13 JavaScript/JScript: Objects Method Description Example abs( x ) absolute value of x if x > 0 then abs(
More informationUNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS
UNIVERSITY OF ENGINEERING & MANAGEMENT, KOLKATA C ASSIGNMENTS All programs need to be submitted on 7th Oct 206 by writing in hand written format in A4 sheet. Flowcharts, algorithms, source codes and outputs
More informationJavaScript: The Basics
JavaScript: The Basics CISC 282 October 4, 2017 JavaScript A programming language "Lightweight" and versatile Not universally respected Appreciated in the web domain Adds programmatic functionality to
More informationNamma Kalvi.
Namma Kalvi COMPUTER APPLICATION PUBLIC EXAM - 2019 ANSWER KEY PART - A I Choose the correct answer 10x1=10 1. c. warm booting 6. d. All the above 11. d..css 2. c. Giga 7. d. 2 12. b. F5 3. b. VGA connector
More informationJavaScript CS 4640 Programming Languages for Web Applications
JavaScript CS 4640 Programming Languages for Web Applications 1 How HTML, CSS, and JS Fit Together {css} javascript() Content layer The HTML gives the page structure and adds semantics Presentation
More informationLesson 7: If Statement and Comparison Operators
JavaScript 101 7-1 Lesson 7: If Statement and Comparison Operators OBJECTIVES: In this lesson you will learn about Branching or conditional satements How to use the comparison operators: ==,!=, < ,
More informationRecursion. Recursion is: Recursion splits a problem:
Recursion Recursion Recursion is: A problem solving approach, that can... Generate simple solutions to... Certain kinds of problems that... Would be difficult to solve in other ways Recursion splits a
More informationSTUDENT LESSON A14 Boolean Algebra and Loop Boundaries
STUDENT LESSON A14 Boolean Algebra and Loop Boundaries Java Curriculum for AP Computer Science, Student Lesson A14 1 STUDENT LESSON A14 Boolean Algebra and Loop Boundaries INTRODUCTION: Conditional loops
More informationMethods: A Deeper Look
www.thestudycampus.com Methods: A Deeper Look 6.1 Introduction 6.2 Program Modules in Java 6.3 static Methods, static Fields and ClassMath 6.4 Declaring Methods with Multiple Parameters 6.5 Notes on Declaring
More informationMethods: A Deeper Look
1 2 7 Methods: A Deeper Look OBJECTIVES In this chapter you will learn: How static methods and variables are associated with an entire class rather than specific instances of the class. How to use random-number
More informationClasswork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015
Classwork 7: Craps N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 For this classwork, you will be writing code for the game Craps. For those of you who do not know, Craps is a dice-rolling
More informationGE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING
GE8151 - PROBLEM SOVING AND PYTHON PROGRAMMING Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING 1) Define Computer 2) Define algorithm 3) What are the two phases in algorithmic problem solving? 4) Why
More informationChapter 3 DATA REPRESENTATION
Page1 Chapter 3 DATA REPRESENTATION Digital Number Systems In digital systems like computers, the quantities are represented by symbols called digits. Many number systems are in use in digital technology
More informationحميد دانشور H_danesh_2000@yahoo.com 1 JavaScript Jscript VBScript Eg 2 JavaScript: the first Web scripting language, developed by Netscape in 1995 syntactic similarities
More informationPrinceton University COS 333: Advanced Programming Techniques A Subset of JavaScript
Princeton University COS 333: Advanced Programming Techniques A Subset of JavaScript Program Structure function sqr(i) var result; // Otherwise result would be global. result = i * i; //
More informationCHW 261: Logic Design
CHW 261: Logic Design Instructors: Prof. Hala Zayed Dr. Ahmed Shalaby http://www.bu.edu.eg/staff/halazayed14 http://bu.edu.eg/staff/ahmedshalaby14# Slide 1 Slide 2 Slide 3 Digital Fundamentals CHAPTER
More informationCourse Schedule. CS 221 Computer Architecture. Week 3: Plan. I. Hexadecimals and Character Representations. Hexadecimal Representation
Course Schedule CS 221 Computer Architecture Week 3: Information Representation (2) Fall 2001 W1 Sep 11- Sep 14 Introduction W2 Sep 18- Sep 21 Information Representation (1) (Chapter 3) W3 Sep 25- Sep
More information