Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-12-2012, 07:10 AM   PM User | #1
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
HTML form and Javascript

Hello
I have attempted to make a Simple Interest Calculator using html forms and javascript and got stuck some where.Here the main problem is alert box does not pop up when i click the calculate interest button

Code:
<html>
<head>Calculator Simple Interest
<script>
var p=document.getElementById("pa");
var n=document.getElementById("ny");
var r=document.getElementById("ri");
function si()
{
var s;
s=(p*n*r)/100;
alert("Total Amount is"+s);
}
function ta()
{
var t;
t=p+s;
alert("Total Amount is"+t);
}
</script>
</head>
<title>Simple Interest Calculator</title>
<body>
<form>
<b>Principal Amount</b><input type="number" id="pa">
<b>No. of years</b><input type="number" id="ny">
<b>Rate of interest</b><input type="number" id="ri">
<input type="button" onclick="si()" value="Calculate Interest">
<input type="button" onclick="ta()" value="Final Amount">
</form>
</body>
</html>

Last edited by adityavishnu; 11-13-2012 at 08:09 AM..
adityavishnu is offline   Reply With Quote
Old 11-12-2012, 07:50 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Your main problems are that you are trying to get the values of p, n and r as soon as the page loads (when they do not even exist at that moment) instead of when the function runs.
Also, the values calculated by one function are not somehow retained for use by the second function - you must work out the interest again. Once a function ends the values of any local variables ae lost.
Also, values entered by the user are strings unless converted to numbers.

Code:
<html>
<head>Calculator Simple Interest
<script>
function si() {
var p=Number(document.getElementById("pa").value);
var n=Number(document.getElementById("ny").value);
var r=Number(document.getElementById("ri").value);
var s=p*n*r/100;
alert("Total Interest Amount is "+s.toFixed(2));
}
function ta() {
var p=Number(document.getElementById("pa").value);
var n=Number(document.getElementById("ny").value);
var r=Number(document.getElementById("ri").value);
var s=p*n*r/100;
var t = p+s;
alert("Total Amount Payable is "+t.toFixed(2));
}
</script>
</head>
<title>Simple Interest Calculator</title>
<body>
<form>
<b>Principal Amount</b><input type="number" id="pa">
<b>No. of years</b><input type="number" id="ny">
<b>Rate of interest</b><input type="number" id="ri">
<input type="button" onclick="si()" value="Calculate Interest">
<input type="button" onclick="ta()" value="Final Amount">
</form>
</body>
</html>
"What's the three words you never want to hear while making love? Honey, I'm home." - Ken Hammond.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
adityavishnu (11-13-2012)
Old 11-12-2012, 08:26 AM   PM User | #3
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
hey ,ma idea is to declare p,n,r as global variable and then use it in functions to calculate simple interest and total amount

Last edited by adityavishnu; 11-12-2012 at 09:42 AM..
adityavishnu is offline   Reply With Quote
Old 11-12-2012, 10:53 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by adityavishnu View Post
hey ,ma idea is to declare p,n,r as global variable and then use it in functions to calculate simple interest and total amount
Yes, you could do that, but many people think it is a good idea to avoid using global variables as far as possible. And in this case it would not achieve anything as if the user clicked "Final Amount" before clicking "Calculate Interest" then an error would be generated. Can you see why?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-12-2012 at 10:57 AM..
Philip M is offline   Reply With Quote
Old 11-12-2012, 11:17 AM   PM User | #5
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
I don't know how to declare it as global variable but whats the matter with this case if p n r are declared globally

Code:
var p=Number(document.getElementById("pa").value);
var n=Number(document.getElementById("ny").value);
var r=Number(document.getElementById("ri").value);
function si() 
{
var s=p*n*r/100;
alert("Total Interest Amount is "+s.toFixed(2));
}

function ta() {
var s=p*n*r/100;
var t = p+s;
alert("Total Amount Payable is "+t.toFixed(2));
}
adityavishnu is offline   Reply With Quote
Old 11-12-2012, 12:02 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by adityavishnu View Post
I don't know how to declare it as global variable but whats the matter with this case if p n r are declared globally

Code:
var p=Number(document.getElementById("pa").value);
var n=Number(document.getElementById("ny").value);
var r=Number(document.getElementById("ri").value);
function si() 
{
var s=p*n*r/100;
alert("Total Interest Amount is "+s.toFixed(2));
}

function ta() {
var s=p*n*r/100;
var t = p+s;
alert("Total Amount Payable is "+t.toFixed(2));
}
If you do not know how to declare p, n and r as global variables then indeed you may not be able to see the problem.

Declare p, n and r as global variables (look up how to do this very basic thing) and then test your script using alerts to inspect the value of the various variables as the script executes. And/or use your error console to identify errors.

I'll say it one more time - Your main problems are that you are trying to get the values of p, n and r as soon as the page loads (when they do not even exist at that moment) instead of when the function runs.

I gave you a full solution in Post #2. If that displeases you in some way, then that is OK by me. But if you are unwilling to take advice then it is pointless asking for help so you must proceed on your own.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 11-12-2012 at 12:05 PM..
Philip M is offline   Reply With Quote
Old 11-13-2012, 08:07 AM   PM User | #7
adityavishnu
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
adityavishnu is an unknown quantity at this point
hey i understood what u said....
kindly tell me which error console to use...that will make it very much easy to program
thank you
adityavishnu is offline   Reply With Quote
Old 11-13-2012, 09:04 AM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by adityavishnu View Post
hey i understood what u said....
kindly tell me which error console to use...that will make it very much easy to program
thank you
All browsers now have a full JavaScript debugger built in (except Firefox which has several extensions available to add a debugger such as Firebug). So you can even go beyond the error console and step through your code one command at a time and see exactly what gets updated and what the values it uses are.

Press F12 in IE and Chrome to access the developer tools.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
forms, html, interest, javascript, simple

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:59 PM.


Advertisement
Log in to turn off these ads.