Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: *Sample Development Loan Calculator.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 8 - Interest Calculator Application: Introducing.
Computer Science 1620 Programming & Problem Solving.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 3b Standard Input and Output Sample Development.
Clearly Visual Basic: Programming with Visual Basic 2008
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
Loop Exercise 1 Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will.
CH Programming An introduction to programming concepts.
A453 Exemplar Password Program using VBA
Visual Basic I Programming
Lesson 1: Introduction to ABAP OBJECTS Todd A. Boyle, Ph.D. St. Francis Xavier University.
Chapter 10 Creating Pop-Up Windows, Adding Scrolling Messages, and Validating Forms HTML5 & CSS 7 th Edition.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Chapter 4: The Selection Structure
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
A right triangle is shown below. The triangle is dilated by a scale factor of 3.5 to create a new triangle. What is the area of the new triangle? A. 24.
Program 6 Any questions?. System.in Does the opposite of System.out.
Writing Program Code in BASIC Write a program to prompt for and accept values into TWO variables, numx and numy. The program should square the value stored.
Function Problems. Write Functions Asks users whether the user wants to continue of not, then returns the answer. Takes two integers and returns 1 if.
CSCI N341: Client-Side Web Programming Copyright ©2004  Department of Computer & Information Science Writing JavaScript Functions.
Previously Repetition Structures While, Do-While, For.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Copyright ©2005  Department of Computer & Information Science JavaScript Modularity.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
22/11/ Selection If selection construct.
1 Program Planning and Design Important stages before actual program is written.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
For Friday Finish reading chapter 9 WebCT quiz 17.
CIS 115 All Exercises Devry University (Devry) FOR MORE CLASSES VISIT CIS 115 All Exercises Devry University.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Introduction to Programming
Introduction to Programming
Data Types and Conversions, Input from the Keyboard
Visual Basic I Programming
Value-Returning Functions
Tutorial 9 - Car Payment Calculator Application Introducing the while Repetition Statement Outline 9.1 Test-Driving the Car Payment Calculator Application.
The Selection Structure
CIS115 Education for Service-- snaptutorial.com
CIS 115 Teaching Effectively-- snaptutorial.com
For Monday Read WebCT quiz 18.
Topics Introduction to File Input and Output
For Wednesday No new reading No quiz.
Libraries of Code Notes from Wilson, Software Design and Development Preliminary Course pp
Message, Input, and Confirm Dialogs
We are starting JavaScript. Here are a set of examples
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Python Basics with Jupyter Notebook
7 – Variables, Input and Output
Topics Introduction to File Input and Output
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Switch Case Structures
Presentation transcript:

Writing JavaScript Functions

Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules. How to develop JavaScript functions using a well-defined, well-planned methodology. How to identify module inputs, processes and outputs.

Steps for Writing Functions 1. Identify the inputs, processes and outputs that your main function will handle. 2. Identify the inputs, processes and outputs that other functions will handle. 3. Develop pseudocode for each of your functions. 4. Code your program.

Guidelines for Writing Functions Have your main function handle (a) getting initial inputs from the user, (b) processing of those inputs (if necessary) and (c) outputs to the user ONLY. Your main function should do little else. Keep function length short – about 1 editor screen per function, give or take. Use the main function as a mechanism for communicating among functions.

Let’s Try One! PROBLEM: “Develop an application for car loans. Your application will ask for the sales price, down payment amount, APR and the length of the loan (in months). Using the formula on the next screen, your program will return a monthly payment.”

Formula for Monthly Payment

Step 1a – Identify Inputs for main() Yes/No to question “Do you want to calculate the monthly payment for a car loan?” (Validate for Yes/No) Sales Price (Validate for Number) Down Payment (Validate for Number) APR (Validate for Number) Length of Loan (In Months; Validate for Number)

Step 1b – Identify Processes for main() Call Yes/No validation function Call number validation function Calculate Loan Amount: Car Price – Down Payment Convert APR from percentage format to floating point number: APR / 100 Call function to calculate monthly payment Concatenate output message

Step 1c – Output for main() Output a message that includes loan amount, APR, monthly payment and loan length.

Step 2 for Yes/No Validator Function Inputs: ◦ Question to Ask (get from main()) ◦ Default Text (get from main()) Processes ◦ Ask user the question and see if their answer did not equal “YES” AND did not equal “NO.” ◦ If the user didn’t answer “YES” or “NO”, let them know that “YES” and “NO” are the only acceptable answers. Ask the user the question again until they answer “YES” or “NO.” Outputs ◦ Give the validated answer back to main().

Step 2 for Number Validator Function Inputs: ◦ Question to Ask (get from main()) ◦ Default Text (get from main()) Processes ◦ Ask the user to enter a number and check to see if they entered data that can be converted to a number. ◦ If the user didn’t enter numeric data, let them know that the program only accepts numbers. Ask the user the question again until they enter numeric data. Outputs ◦ Give the validated answer back to main().

Step 2 for Calculate Monthly Payment Function Inputs: ◦ Loan Amount ◦ APR ◦ Length of the Loan, in months Processes ◦ Figure the monthly interest: APR / 12 ◦ Use the formula below to calculate monthly payment Outputs ◦ Give the monthly payment back to main().

Step 3: Develop Pseudocode NOTE: Instructor will give you pseudocode in class.

Step 4: Code the Program Finished Code:

Let’s Try Another One! PROBLEM: “Develop an application to calculate a student’s semester grade. Your application will ask for the scores from three exams. The application will then calculate the average of the exam scores and return the average along with a letter grade. The teacher uses a standard grading scale.”

The Coded Product Finished Code:

Commenting Functions In your code, you need to provide descriptive comments for each of your functions. When writing function comments, include the following: ◦ A brief (less than 1 line) description of the function ◦ A list of arguments for the function ◦ A list of values returned by the function ◦ A statement of the function’s purpose

Commenting Functions Example: /*FUNCTION: calcAvg() * *PARAMETERS: IntX, IntY, IntZ – All *integer numbers * *RETURNS: The calculated average; *stored in local variable rtnAvg * *PURPOSE: To calculate the average *of three integer values, passed *by a calling procedure and then *return that average to the procedure. */

Questions?