JavaScript Function Example Please use speaker notes for additional information.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

JavaScript Functions Please use speaker notes for additional information!
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
The Web Warrior Guide to Web Design Technologies
Lesson 4: Formatting Input Data for Arithmetic
Introduction to JavaScript Please see speaker notes for additional information!
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Lecture Note 3: ASP Syntax.  ASP Syntax  ASP Syntax ASP Code is Browser-Independent. You cannot view the ASP source code by selecting "View source"
CSC318 – DYNAMIC WEB APPLICATION DEVELOPMENT
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 7 - JavaScript: Introduction to Scripting Outline 7.1 Introduction 7.2 Simple Program: Printing.
1 Accelerated Web Development Course JavaScript and Client side programming Day 2 Rich Roth On The Net
What is Java Script? An extension to HTML. An extension to HTML. Allows authors to incorporate some functionality in their web pages. (without using CGI.
Client Side Programming with JavaScript Why use client side programming? Web sides built on CGI programs can rapidly become overly complicated to maintain,
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
Computers and Scientific Thinking David Reed, Creighton University Functions and Libraries 1.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
Java Script User Defined Functions. Java Script  You can define your own JavaScript functions. Such functions are called user- defined, as opposed to.
David Stotts Computer Science Department UNC Chapel Hill.
CS346 Javascript -3 Module 3 JavaScript Variables.
THINKING BIG Abstraction and Functions chapter 6 Modified by Dr. Paul Mullins for CPSC 130, F05.
Variety of JavaScript Examples Please use speaker notes for additional information!
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 8 - JavaScript: Control Structures I Outline 8.1 Introduction 8.2 Algorithms 8.3 Pseudocode 8.4.
Dr. Qusai Abuein1 Internet & WWW How to program Chap.(6) JavaScript:Introduction to Scripting.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introduction to Programming JScript Six Scripting functions Discuss functions Password Example.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
JavaScript Loops Please use speaker notes for additional information!
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
1 Chapter 3 – JavaScript Outline Introduction Flowcharts Control Structures if Selection Structure if/else Selection Structure while Repetition Structure.
JavaScript Challenges Answers for challenges
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
JavaScript Functions A function is a block of code that can be reused. It is similar to a method in Java. It consists of a function name, an argument.
Functions.  Assignment #2 is now due on Wednesday, Nov 25 th  (No Quiz)  Go over the midterm  Backtrack and re-cover the question about tracing the.
Functions (doing a calculation) Please use speaker notes for additional information!
Chapter 2 Murach's JavaScript and jQuery, C2© 2012, Mike Murach & Associates, Inc.Slide 1.
Unit 10-JavaScript Functions Instructor: Brent Presley.
Understanding JavaScript and Coding Essentials Lesson 8.
MTA EXAM HTML5 Application Development Fundamentals.
Answer questions about assignment.. Starting JavaScript, at my site these examples are under programs and JavaScript. You can see the address for this.
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.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
JavaScript 101 Lesson 6: Introduction to Functions.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
INTERNET APPLICATIONS CPIT405 JavaScript Instructor: Rasha AlOmari
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
JAVA Script : Functions Ashima Wadhwa
Functions Comp 205 Fall ‘17.
Programming the Web using XHTML and JavaScript
Exploring JavaScript Ch 14
Chapter 8 - JavaScript: Control Structures I
A second look at JavaScript
Functions, Part 2 of 2 Topics Functions That Return a Value
We are starting to program with JavaScript
We are starting JavaScript. Here are a set of examples
An Introduction to JavaScript
Note the rights settings.
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.
JavaScript CS 4640 Programming Languages for Web Applications
Brent M. Dingle Texas A&M University Chapter 5 – Section 2-3
Chapter 8 - JavaScript: Control Structures I
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Murach's JavaScript and jQuery (3rd Ed.)
JavaScript CS 4640 Programming Languages for Web Applications
Scope Rules.
Presentation transcript:

JavaScript Function Example Please use speaker notes for additional information.

Note the difference in execution in Netscape 6.2 and Explorer. The H1 at the beginning of the BODY appears prior to the prompts in Explorer. avgfunca.html

Function to calculate average function calAvg(exam, proj, hmwk) { theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); avgfunca.html The function is called and the data that was passed in is sent to the function. The data that is sent is defined in the SCRIPT outside the function and is therefore global. The data is received using the same names. The function calculates the average in theAvg and shows the answer.

avgfuncaa.html Function to calculate average function calAvg(exam, proj, hmwk) { theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams", "0")); var proj = parseInt(prompt("Key in the grade for projects", "0")); var hmwk = parseInt(prompt("Key in the grade for homework", "0")); calAvg(exam, proj, hmwk);

avgfuncb.html

Function to calculate average function calAvg(exam, proj, hmwk) { theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); You should note that when I define theAvg using var this does not work because theAvg in the body does not see it. That is because variables defined within a function are local. If you define them outside the function they are global. Omitting the word var gets around this but is not considered a great coding technique. avgfuncb.html

avgfuncc.html

Function to calculate average function calAvg(exam, proj, hmwk) { var theAvg = (exam + proj + hmwk)/3; document.write("The average is " + theAvg); } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); You should note that when I define theAvg using var in the function, I cannot see the document write of the answer from the script in the Body. That is because variables defined within a function are local. If you define them outside the function they are global. Omitting the word var gets around this but is not considered a great coding technique. avgfuncc.html

avgfuncd.html

Function to calculate average function calAvg(exam, proj, hmwk) { var theAns = (exam + proj + hmwk)/3; document.write("The average is " + theAns); theAvg = theAns; } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it.

avgfunce.html

Function to calculate average function calAvg() { var theAns = (exam + proj + hmwk)/3; document.write("The average is " + theAns); theAvg = theAns; } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(); document.write(" The average is " + theAvg); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it. Note that when I define the variables as I did, outside the function, I do not need to pass. Preferred methodology is to pass. In fact, usually you use separate names to maintain the integrity of the data. avgfunce.html

avgfuncf.html

Function to calculate average function calAvg(calExam, calProj, calHmwk) { var theAns = (calExam + calProj + calHmwk)/3; document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); theAvg = theAns; document.write(" Last line in the function "); } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it. Here I am using separate names for the data inside the function and outside the function to maintain the integrity of the data.

avgfuncg.html

Function to calculate average function calAvg(calExam, calProj, calHmwk) { calExam = parseInt(calExam * 1.1); calProj = parseInt(calProj * 1.1); calHmwk = parseInt(calHmwk * 1.1); var theAns = parseInt((calExam + calProj + calHmwk)/3); document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); theAvg = theAns; document.write(" Last line in the function "); } Calculate Average var theAvg = 0; var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); calAvg(exam, proj, hmwk); document.write(" The average is " + theAvg); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I defined theAvg outside the function which made it global. I defined theAns inside the function and then assigned the results to theAvg as I left the function. Since it is global I can then see it. Here I am using separate names for the data inside the function and outside the function to maintain the integrity of the data. I am also changing the value of the data after it has been passed.

avgfunch.html

Function to calculate average function calAvg(calExam, calProj, calHmwk) { calExam = parseInt(calExam * 1.1); calProj = parseInt(calProj * 1.1); calHmwk = parseInt(calHmwk * 1.1); var theAns = parseInt((calExam + calProj + calHmwk)/3); document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); document.write(" Last line in the function "); return theAns; } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); theAns = calAvg(exam, proj, hmwk); document.write(" The average is " + theAns); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I am calculating theAns and then returning it. Note I could have defined a var called theAvg here in the BODY script and then assigned theAns to theAvg and displayed theAvg.

avgfunci.html

Function to calculate average function calAvg(calExam, calProj, calHmwk) { calExam = parseInt(calExam * 1.1); calProj = parseInt(calProj * 1.1); calHmwk = parseInt(calHmwk * 1.1); var theAns = parseInt((calExam + calProj + calHmwk)/3); document.write("Inside the function"); document.write(" The average is " + theAns); document.write(" " + calExam + " " + calProj + " " + calHmwk); document.write(" Last line in the function "); return theAns; } Calculate Average var exam = parseInt(prompt("Key in the grade for exams")); var proj = parseInt(prompt("Key in the grade for projects")); var hmwk = parseInt(prompt("Key in the grade for homework")); document.write(" The average is " + calAvg(exam, proj, hmwk)); document.write(" " + exam + " " + proj + " " + hmwk); document.write(" Note the variables used in the function are not available ") document.write(calExam + " " + calProj + " " + calHmwk); In this example I am calculating theAns and then returning it. Note that I call the function within the write.