writing javascript functions. goals by the end of this unit, you should understand … how to...

19
Writing JavaScript Writing JavaScript Functions Functions

Upload: alban-turner

Post on 04-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

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

Writing JavaScript Writing JavaScript FunctionsFunctions

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

GoalsGoalsBy 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.

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

Steps for Writing Steps for Writing FunctionsFunctions1. 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.

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

Guidelines for Writing Guidelines for Writing FunctionsFunctionsHave 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.

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

Let’s Try One!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.”

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

Formula for Monthly Formula for Monthly PaymentPayment

LoanLengthMoIntRate)(11

MoIntRate*LoanAmtMoPmt

LoanLengthMoIntRate)(11

MoIntRate*LoanAmtMoPmt

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

Step 1a – Identify Inputs for Step 1a – Identify Inputs for main()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)

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

Step 1b – Identify Step 1b – Identify Processes for main()Processes for main()Call Yes/No validation functionCall number validation functionCalculate Loan Amount:

Car Price – Down PaymentConvert APR from percentage format

to floating point number:APR / 100

Call function to calculate monthly payment

Concatenate output message

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

Step 1c – Output for Step 1c – Output for main()main()Output a message that includes

loan amount, APR, monthly payment and loan length.

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

Step 2 for Step 2 for Yes/No Validator FunctionYes/No Validator FunctionInputs:

◦ 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().

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

Step 2 for Step 2 for Number Validator FunctionNumber Validator FunctionInputs:

◦ 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().

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

Step 2 for Calculate Monthly Step 2 for Calculate Monthly Payment FunctionPayment 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().

LoanLengthMoIntRate)(11

MoIntRate*LoanAmtMoPmt

LoanLengthMoIntRate)(11

MoIntRate*LoanAmtMoPmt

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

Step 3: Develop Step 3: Develop PseudocodePseudocodeNOTE: Instructor will give you

pseudocode in class.

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

Step 4: Code the ProgramStep 4: Code the Program

Finished Code:

http://www.cs.iupui.edu/~rmolnar/n341/examples/n341WritingFunctions.html

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

Let’s Try Another One!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.”

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

The Coded ProductThe Coded Product

Finished Code:

http://www.cs.iupui.edu/~rmolnar/n341/examples/n341FunctionsPassingValues.html

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

Commenting FunctionsCommenting FunctionsIn 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

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

Commenting FunctionsCommenting FunctionsExample:

/*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. */

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

Questions?Questions?