5 th and 4 th ed: Chapters 9, 10, 11 SY306 Web and Databases for Cyber Operations SlideSet #7: JavaScript Functions and Arrays.

Slides:



Advertisements
Similar presentations
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Advertisements

JavaScript Part 6. Calling JavaScript functions on an event JavaScript doesn’t have a main function like other programming languages but we can imitate.
1 JavaScript: Control Structures II. 2 whileCounter.html 1 2
1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
Javascript Client-side scripting. Up to now  We've seen a little about how to control  content with HTML  presentation with CSS  Javascript is a language.
Data Structures Week 4 INFM 603. The Key Ideas Structured Programming  Modular Programming  Data Structures Object-Oriented Programming.
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Functions Part I.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
1 JavaScript/Jscript 4 Functions. 2 Introduction Programs that solve real-world programs –More complex than programs from previous chapters Best way to.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - JavaScript: Functions Outline 10.1 Introduction 10.2 Program Modules in JavaScript 10.3.
Arrays – What is it? – Creation – Changing the contents Functions – What is it? – Syntax – How they work – Anonymous functions A quick lesson in Objects.
Chapter 6: More JavaScript CIS 275—Web Application Development for Business I.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
CHAPTER 4 Java Script อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Chapter 2 User_defined Function. Chapter Goals In this chapter, you’ll learn all about PHP functions, including how : to create and invoke them, pass.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
CS346 Javascript -3 Module 3 JavaScript Variables.
4.4 JavaScript (JS) Deitel Ch. 7, 8, 9, JavaScript & Java: Similarities JS (JavaScript) is case-sensitive Operators –arithmetic: unary +, unary.
 2001 Deitel & Associates, Inc. All rights reserved. 1 Outline 16.1Introduction 16.2Program Modules in JavaScript 16.3Programmer-Defined Functions 16.4Function.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Basic Data Types Numbers (integer and floating point)‏ Strings (sequences of characters)‏ Boolean values (true/false)‏
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Python Functions.
Introduction to Programming JScript Six Scripting functions Discuss functions Password Example.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CS 261 – Data Structures Introduction to C Programming.
JavaScript, Fourth Edition
Chapter 10: JavaScript Functions CIS 275—Web Application Development for Business I.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
Chapter 11: Arrays CIS 275—Web Application Development for Business I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Lesson 16. Practical Application 1 We can take advantage of JavaScript and the DOM, to set up a form so that the first text box of a form automatically.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
Adapted from  2004 Prentice Hall, Inc. All rights reserved. 5 th and 4 th ed: Chapters 6,7,8 SY306 Web and Databases for Cyber Operations SlideSet #6:
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
JavaScript and Ajax (JavaScript Functions) Week 5 Web site:
Expressions and Data Types Professor Robin Burke.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
JavaScript and Ajax Week 10 Web site:
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
JavaScript Programming. What Is JavaScript “the most popular programming language in the world”— w3schools.com “the language for HTML, the Web, computers,
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
Chapter 11 - JavaScript: Arrays
>> Introduction to JavaScript
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Chapter 19 JavaScript.
Web Systems Development (CSC-215)
G. Pullaiah College of Engineering and Technology
Presentation transcript:

5 th and 4 th ed: Chapters 9, 10, 11 SY306 Web and Databases for Cyber Operations SlideSet #7: JavaScript Functions and Arrays

Function Definitions Syntax and terminology: function function-name ( parameter-list ) { declarations and statements } Example /* Returns the sum of x and y */ function doAdd(x, y) { var sum = x + y; return sum; }

Function Invocation Built-in functions User-defined functions Arguments are passed ______________, so original values in caller are ________________

Scope – Where is a variable visible in the program? function dog(g) { h = 3; var sum = g+h; document.write(" Sum is: "+sum); } g = 7; h = 5; document.writeln(" g: "+g+" h: "+h); dog(g); document.writeln(" g: "+g+" h: "+h); document.writeln(" sum: "+sum); document.writeln(“ End of script"); Output?

JavaScript Scope Rules Variables declared inside a function: –Explicitly (with var) –Implicitly (just used) - Consider if variable used should be local or global? Variables declared outside a function: –Explicitly –Implicitly

Exercise #1 – Write a function that takes two arguments and returns the minimum of the two; write code to invoke the function

Exercise #2 – What’s the output? function fun1 (x) { x = x + 3; y = y + 4; document.writeln(" FUN1: "+x+ "," +y); } function fun2 () { var y; x = x + 10; y = y + 20; document.writeln(" FUN2: "+x+ "," +y); } x = 1; y = 2; document.writeln(" MAIN #1: "+x+ "," +y); fun1(x); document.writeln(" MAIN #2: "+x+ "," +y); fun1(y); document.writeln(" MAIN #3: "+x+ "," +y); fun2(); document.writeln(" MAIN #4: "+x+ "," +y);

Exercise #3 – Write a function indentPrint(N, str1, str2) that outputs the following: a.) ‘N’ dashes, followed by the string ‘str1’, then b.) ‘N’ dashes, followed by the string ‘str2’, then Use document.write() for output. You can assume N is an integer.

Connecting JavaScript and HTML Where to place the JavaScript –In the.html file –In a separate file How to invoke the script? –Place non-function code in the –

Everything you ever wanted to know about arrays… function initializeArrays() { var n1 = new Array( 5 ); // allocate 5-element Array var n2 = new Array(); // allocate empty Array for ( var i = 0; i < n1.length; i++) n1[i] = i; for ( i = 0; i < 5; ++i ) n2[i] = i; outputArray( "Array n1 contains", n1 ); outputArray( "Array n2 contains", n2 ); } function outputArray( header, theArray ) { document.writeln( " " + header + " " ); for ( var num in theArray ) { document.write(theArray[num] + “ " ); } document.writeln( “ " ); } initializeArrays();

…but were afraid to ask.

Scope – Revisited function mystery( x, y ) { for ( var ii = 0; ii < x.length; ++ii ) x[ii] = x[ii] * y; document.writeln(" Revised Array: ",x); } var myArray = [3, 4, 5]; var factor = 2; document.writeln (" myArray: ", myArray); mystery(myArray, factor); document.writeln (" myArray: ", myArray); document.writeln (" factor : ", factor); Arguments are passed ______________, so original argument values in caller are ________________ BUT array/object arguments are a “reference”, so contents may be ___________

Useful Array Functions.push(arg) var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.push("Kiwi"); The result of fruits will be: Banana, Orange, Apple, Mango, Kiwi.indexOf var stuff=new Array('fruit', 'apples'); alert(stuff.indexOf('apples')); //result is 1.sort var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.sort(); //The result of fruits will be: Apple, Banana, Mango, Orange.sort(comparatorFunction) //sorts numbers in array var points = [40, 100, 1, 5, 25, 10]; points.sort(function(a, b){return a-b}); //Result will be ascending: 1, 5, 10, 25, 40, 100 points.sort(function(a, b){return b-a}); //Result will be descending: 100, 40, 25, 10, 5, 1

Exercise #4 a.) Write a function “sumArray” as follows: Input: an array Output: the sum of that array b.) Write test code to create an array and call “sumArray” on it.

JavaScript Secrets Invalid numbers are NaN –Test with isNaN(value) 5 types for variables: –number (including NaN) –string –boolean –“undefined” – may cause error or lead to NaN –null Gotchas –if (x = 7) … –Uninitialized variables –Forgetting “break” in switch

JavaScript Tips Quoting document.writeln(" cat "); vs. document.writeln(" cat "); Multiple arguments to document.write() document.writeln(" "+heading+" "); (doesn’t work with my_writeln() )