Extra Credit - Javascript Financial Calculator

LABAR Extra Credit Project
This assignment is to be done during your one hour lab by arrangement (LABAR). This assignment requires you to write your first javascript to declare, assign and display three variables of the string, number, and boolean types respectively.

In this extra credit assignment, you are to create a compound interest payment calculator as in the example below that takes the loan amount, term, and apr interest in HTML FORM, and have a submit button to calculate the payment. The annual APR interest is a compound calculation, that the annual interest shall become capital. Here is a demo Interest Calculator:



Loan Amount:
Loan Term: years
APR Interest:

Payment:




You may implement the solution using the following code examples:

1. Type cast the input type to number
javascript to read and convert the input text string from coresponding html FORM text INPUT to an integer variable:

var years = parseInt(document.block.par.value);


// parseInt is the function converts string to integer
// document is the built-in object, like document.write
// block is the html form name below
// par is the html input name below

corresponding html:

<form id="block" name="block">
<input type="text" name="par"/>
...
</form>

javascript to write the result to corresponding html FORM text INPUT:

document.blog.par.value=result;

2. html FORM text INPUT
to disallow the user to alter its value:

<input name="name_of_input_field" disabled="disabled" type="text" />

to act as a button on mouse click perform javascript function:

<input onclick="function();" type="submit" value="Label" />


// type has to be "submit"
// "function();" should be "the_name_of_your_function();"
// "Label" should be "the_button_label_to_be_displayed_on_screen"

3. javascript function declaration:

function name_of_the_function(par1,par2,...) {
...
}

4. javascript defensive programing
In case the user enters a text string instead of number, the following script would reset the variable to default value:

if((variable <= 0)||(isNaN(variable))) variable = 1;   // isNaN() returns true if the number variable is not a number // in this example, the default value is 1

5. an example formula is shown in the following example:

for( i=term; i>0; i--){
principal = principal*(1 + apr/100);
}
payment = principal/term;

No response to “Extra Credit - Javascript Financial Calculator”

Post a Comment