•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 422,663 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,654 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 678 | Replies: 4
![]() |
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
I am not a computer programmer but have an assignment that involves javascript. I need a program that will display the interest in a savings account if I deposit $1000 at 5% interest for 10 years. I need it to show the interest each year for each of the 10 years. I can do it with multiplication of 1 through 9 but I can't figure out how to do it with this.
I was given the following info:
a=p(l+r)"n
p is principal
r is annual interest
n is number of years
a is amount on deposit at end of nth year.
Since javascript doesnt recognize exponents I was also given this.
amount = principal * Math.pow(1.0 rate, year);
also use decimals
Math.round(amount * 100)/100
Here is the program that will do multiplication facts for 1 though 9
<HTML>
<head>
<title> Tues Class example 4</title>
<script language="javascript">
function show()
{
var result;
for (var i=1; i<=9; i++)
{
for (var j=1; j<=9; j++)
{
result = j*i;
document.write(i + " x " + j + " = " + result);
}
document.write("<br>");
}
}
</script>
</head>
<body>
<p><input type="button" value="click to see the result" onClick="show()">
</form>
</body>
</HTML>
I was given the following info:
a=p(l+r)"n
p is principal
r is annual interest
n is number of years
a is amount on deposit at end of nth year.
Since javascript doesnt recognize exponents I was also given this.
amount = principal * Math.pow(1.0 rate, year);
also use decimals
Math.round(amount * 100)/100
Here is the program that will do multiplication facts for 1 though 9
<HTML>
<head>
<title> Tues Class example 4</title>
<script language="javascript">
function show()
{
var result;
for (var i=1; i<=9; i++)
{
for (var j=1; j<=9; j++)
{
result = j*i;
document.write(i + " x " + j + " = " + result);
}
document.write("<br>");
}
}
</script>
</head>
<body>
<p><input type="button" value="click to see the result" onClick="show()">
</form>
</body>
</HTML>
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
I do have one question about the above info...
I have been taught to create a button so that onClick it displays the calcuations. Do I need to use "for" in my code to make that possible?
As I stated I am very new to this and not a programmer. So I feel very overwhelmed by all of this.
THis is what I have used in the past...
function show() (which I don't know what this means or is telling the computer to do)
<p><input type="button" value="click ot see the result" onClick="show()">
I tried to add this but its not working either... any ideas?
I have been taught to create a button so that onClick it displays the calcuations. Do I need to use "for" in my code to make that possible?
As I stated I am very new to this and not a programmer. So I feel very overwhelmed by all of this.
THis is what I have used in the past...
function show() (which I don't know what this means or is telling the computer to do)
<p><input type="button" value="click ot see the result" onClick="show()">
I tried to add this but its not working either... any ideas?
•
•
Join Date: Dec 2007
Posts: 75
Reputation:
Rep Power: 1
Solved Threads: 10
Javascript has built-in functions, meaning it already comes with some functions that you can just use. It also lets you create your own functions.
show() is not a built in function.
For the following to work:
<input type="button" value="click to see the result" onClick="show()">
you would need to create a function called show. Within the show function you need to call the while loop I posted previously. The only thing however is that before the while loop you would need to write:
document.open();
and after the while's closing brace you would need:
document.close();
Try it, and let me know what happens.
show() is not a built in function.
For the following to work:
<input type="button" value="click to see the result" onClick="show()">
you would need to create a function called show. Within the show function you need to call the while loop I posted previously. The only thing however is that before the while loop you would need to write:
document.open();
and after the while's closing brace you would need:
document.close();
Try it, and let me know what happens.
•
•
Join Date: Dec 2007
Posts: 75
Reputation:
Rep Power: 1
Solved Threads: 10
Below is a complete example of how I would put the a() function that I gave you to use.
You can call show with the number of years as well as the id of the element where you want the results to appear. Example:
<input type="button" onclick="show(5,'myResults');" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hielo's Calculations</title>
<script type="text/javascript"><!--
function a(p,r,n)
{
return (parseFloat(p) * Math.pow( (1+parseFloat(r)), parseInt(n,10) )).toFixed(2);
}
function show(years,target)
{
var i=1;//starting year
var limit=10;//the default number of years if none specified
var destination=null;//where to write the results
var buffer=[];//to save the partial results
//check if user specified number of years and if so, override default
if(parseInt(years,10))
limit=parseInt(years,10);
//check if user specified target element to write results and if so, override default
if("string"==typeof(target))
{
destination=document.getElementById(target);
}
else//otherwise create a div dinaymically and append it to <body>. The results will be withing <div id='results'></div>
{
if( destination=document.createElement("div") )
{
destination.id="target";
document.getElementsByTagName('body')[0].appendChild(destination);
}
}
while(i<=limit)
{
buffer[buffer.length] = ( i + ". " + a("1000.00",".05",i) );
++i;
}
if(destination)
destination.innerHTML = "<div>"+buffer.join("</div><div>")+"</div>";
else//this execute only in very old browsers.
alert(buffer.join("\n"));
}
//--></script>
</head>
<body>
<form>
<input type="button" value="Compute" onclick="show(10,'myResults');"/>
</form>
<div id="myResults"></div>
</body>
</html>
You can call show with the number of years as well as the id of the element where you want the results to appear. Example:
<input type="button" onclick="show(5,'myResults');" />
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hielo's Calculations</title>
<script type="text/javascript"><!--
function a(p,r,n)
{
return (parseFloat(p) * Math.pow( (1+parseFloat(r)), parseInt(n,10) )).toFixed(2);
}
function show(years,target)
{
var i=1;//starting year
var limit=10;//the default number of years if none specified
var destination=null;//where to write the results
var buffer=[];//to save the partial results
//check if user specified number of years and if so, override default
if(parseInt(years,10))
limit=parseInt(years,10);
//check if user specified target element to write results and if so, override default
if("string"==typeof(target))
{
destination=document.getElementById(target);
}
else//otherwise create a div dinaymically and append it to <body>. The results will be withing <div id='results'></div>
{
if( destination=document.createElement("div") )
{
destination.id="target";
document.getElementsByTagName('body')[0].appendChild(destination);
}
}
while(i<=limit)
{
buffer[buffer.length] = ( i + ". " + a("1000.00",".05",i) );
++i;
}
if(destination)
destination.innerHTML = "<div>"+buffer.join("</div><div>")+"</div>";
else//this execute only in very old browsers.
alert(buffer.join("\n"));
}
//--></script>
</head>
<body>
<form>
<input type="button" value="Compute" onclick="show(10,'myResults');"/>
</form>
<div id="myResults"></div>
</body>
</html>
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Javascript links not working (Web Browsers)
- JavaScript's window.opener (JavaScript / DHTML / AJAX)
- recommendations for JavaScript learning resources? (JavaScript / DHTML / AJAX)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Javascript Useful? (IT Careers and Business)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: get data from php
- Next Thread: Changing Background Color


Linear Mode