Forum Moderators: open
<HEAD>
<TITLE>Interest Rate</TITLE>
<SCRIPT>
var interestRate = 0;
var response;
var amountIn, interest;
function twoDPs(anyNumber)
{
return Math.round (100 * anyNumber) / 100
};
function setInterestRate()
{
};
function displayInterestRate()
{
document.write('Current interest rate is ' + interestRate + '%')
};
function calculateInterest(numberOfPounds)
{
};
// Main program starts here
setInterestRate();
displayInterestRate();
document.write('<BR><BR><BR><BR>');
response = window.prompt('Do you want to calculate the interest on an amount of money (Y/N)? ','');
while(response == 'Y')
{
amountInPounds = window.prompt('Enter amount in Pounds ','');
interest = calculateInterest(amountInPounds);
document.write('£ '+ amountInPounds + ' attracts £' + interest + ' interest');
document.write('<BR><BR><BR>');
response = window.prompt('Do you want to calculate the interest for another amount (Y/N)? ','')
}
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
I am trying to calculate interest rate but it isn't responding
isn't responding
I reckon that's because your
calculateInterest function is totally empty, so it returns undefined. And is it possible that when the user enter y they can continue to use the program
Yes. Convert all input to upper-case:
while(response.toUpperCase() == 'Y') ..although I would recommend
- using the
[b]confirm[/b] method instead, while( confirm("Do you want to calculate...") )
{
...
} [edited by: Bernard_Marx at 12:54 pm (utc) on June 21, 2007]
<SCRIPT>
var interestRate = 0;
var response;
var amountIn, interest;
function twoDPs(anyNumber)
{
return Math.round (100 * anyNumber) / 100
};
function setInterestRate()
{
interestRate = window.prompt('Please enter the rate of Interest Rate of your choosing' + '')
};
function displayInterestRate()
{
document.write('<BR>' + 'Current interest rate is ' + interestRate + '%')
}
function calculateInterest(numberOfPounds) {
interest = numberOfPounds * interestRate / 100;
interest = twoDPs(interest); // or interest = interest.toFixed(2);
}
setInterestRate();
displayInterestRate();
document.write('<BR><BR><BR><BR>');
response = window.prompt('Do you want to calculate the interest on an amount of money (Y/N)? ','');
while(response == 'Y')
{
amountInPounds = window.prompt('Enter amount in Pounds ','');
interest = ((interestRate)*(amountInPounds)/100);
document.write('<BR>' + '£ '+ amountInPounds + ' attracts £' + interest + ' interest');
document.write('<BR><BR><BR>');
response = window.prompt('Do you want to calculate the interest for another amount (Y/N)? ','')
}
</SCRIPT>
document.body.appendChild(document.createElement("br"));
With this method you would be appending data onto the screen rather than overwriting what's already there, leaving the above calculation still visible.
Or if you wanted to move everything on the screen down and keep the last calculation towards the top of the screen:
document.body.insertBefore(document.createTextNode("£" + amountInPounds + " attracts £" + interest + " interest"), document.body.childNodes[0]);