Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.

Slides:



Advertisements
Similar presentations
1 CSC 533: Organization of Programming Languages Spring 2007 Java vs. JavaScript  JavaScript design goals  examples input/output, functions, numbers.
Advertisements

1 CSC 121 Computers and Scientific Thinking David Reed Creighton University Chapter 9 Abstraction and User-Defined Functions.
Alice in Action with Java
Lesson 4: Formatting Input Data for Arithmetic
Static Methods Chapter 4. Chapter Contents Objectives 4.1 Introductory Example: Old MacDonald Had a Farm … 4.2 Getting Started with Methods 4.3 Example:
JavaScript 101 Lesson 01: Writing Your First JavaScript.
Computer Science 103 Chapter 3 Introduction to JavaScript.
HTML Comprehensive Concepts and Techniques Second Edition Project 8 Integrating JavaScript with HTML.
Alice in Action with Java
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
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.
JavaScript Lesson 1 TBE 540. Prerequisites  Before beginning this lesson, the learner must be able to… Create a basic web page using a text editor and/or.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Introduction to scripting
PHP: Hypertext Processor Fred Durao
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Week 9 PHP Cookies and Session Introduction to JavaScript.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
1 Functions Lecfture Abstraction abstraction is the process of ignoring minutiae and focusing on the big picture in modern life, we are constantly.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
Keeping it Neat: Functions and JavaScript Source Files Chapter 7.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University JavaScript and Dynamic Web Pages.
CSC 121 Computers and Scientific Thinking David Reed Creighton University 1 Abstraction and User-Defined Functions.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
JavaScript III Functions and Abstraction. 2 JavaScript so far statements assignment function calls data types numeric string boolean expressions variables.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
Input & Output Functions JavaScript is special from other languages because it can accept input and produce output on the basis of that input in the same.
Introduction to JavaScript CS101 Introduction to Computing.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
JavaScript, Fourth Edition
1 Functions, Part 1 of 2 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function Header Comments.
1 JavaScript Part 3. Functions Allow the user to decide when a particular script should be run by the browser in stead of running as long as the page.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Variables (Alice In Action, Ch 3) Slides Credit: Joel Adams, Alice in Action CS120 Lecture 04 7 September 2012.
Enhancing Websites with JavaScript. How Do You Give Directions? Lots of details? Specific steps? Just the gist? What language?
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Unit 10-JavaScript Functions Instructor: Brent Presley.
Javascript Overview. What is Javascript? May be one of the most popular programming languages ever Runs in the browser, not on the server All modern browsers.
Methods.
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
CSC 121 Computers and Scientific Thinking Fall Interactive Web Pages.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Abstraction and Functions Professor Robin Burke. 2 Outline Quiz JavaScript review Abstraction Function Definitions purpose syntax Functions return value.
Java Script Programming. Review: Event Handling Text Box Title: Button.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
1 JavaScript and Dynamic Web Pages Lecture 7. 2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.
1 CSC 121 Computers and Scientific Thinking David Reed Creighton University JavaScript and Dynamic Web Pages.
JavaScript: A short introduction Joseph Lee Created by Joseph Lee.
Moving away from alert() Using innerHTML Using an empty div section
Abstraction and Functions
JAVA Script : Functions Ashima Wadhwa
Donna J. Kain, Clarkson University
Barb Ericson Georgia Institute of Technology May 2006
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Programming the Web using XHTML and JavaScript
For this assignment, copy and past the XHTML to a notepad file with the .html extension. Then add the code I ask for to complete the problems.
Chapter 20: Programming Functions
Abstraction and User-Defined Functions
JavaScript: Introduction to Scripting
Chapter 9 Abstraction and User-Defined Functions
Chapter 7 Abstraction and User-Defined Functions
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Functions, Part 1 of 2 Topics Using Predefined Functions
Presentation transcript:

Java Script User Defined Functions

Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to predefined functions which you get from a library, such as a Date() What is important to know about user-defined functions:  Functions allow you to group a set of statements and give them a name.  Functions are defined in the head of a document.  When you define a function, the statements inside are not executed.  Later you invoke the function in the body of the document, and the statements inside execute.  A function may have inputs (for instance, Math.sqrt() has one input). Values of inputs are sent to the function in parentheses when the function is invoked.  A function may return a value, like the promprt() and Math.random() functions.

Example Feet to Meters function displayMeters(feet) { // the function takes distance in feet // and displays the equivalent distance in meters document.write(" You entered " + feet + " feet. "); // meters is a local variable: var meters = feet * ; document.write("That's equivalent to " + meters + " meters."); } var feet = prompt("Please enter the distance measured in feet", ""); displayMeters(feet); feet= prompt("Give me one more -distance in feet-"); displayMeters(feet);

Answer these questions 1.What is the name of the function that converts the distance? 2.Where do we specify the formula for the conversion? 3.Where is the function defined, and where invoked? 4.When is it invoked? How many times? 5.What is the input (or argument or parameter) of the function? 6.How is the result displayed on the web page?

Or…  Alternatively, you can write a function that computes and returns the result. In this case the function itself will not print anything in the page, the printing is done in the body of the document.

Feet to Meters function convert(feet) { // the function takes distance in feet // and returns this distance in meters return feet *0.3048; } feet = prompt("Please enter the distance measured in feet", ""); document.write(" You entered " + feet + " feet. "); document.write("That's equivalent to " + convert(feet) + " meters.");

Old MacDonald 1.Remember the Old MacDonald song? The verses of the Old MacDonald song are all the same. The only thing that changes from verse to verse, is the animal and the sound. Use the example on the next slide and paste into a new file OldMac.html and fill in the code for a JavaScript function OldMacVerse that displays one verse of the Old MacDonald song. 2.Since we invoke the function 4 times with different animals and sounds (see the example below), we get a page that displays the entire song! The resulting page should look like this.this

Check for this Old MacDonald and New Functions! function OldMacVerse(animal, sound) // Assumes: animal and sound are strings // Results: displays corresponding Old MacDonald verse { // your code goes here: } OldMacVerse("cow", "moo"); OldMacVerse("pig", "oink"); OldMacVerse("duck", "quack"); OldMacVerse("horse", "neigh");