Forum Moderators: open

Message Too Old, No Replies

Interest Rate

         

java_junkie

12:13 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



<HTML>

<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

java_junkie

12:36 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



And is it possible that when the user enter y they can continue to use the program because the program only accept Y

Bernard Marx

12:53 pm on Jun 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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,
- and putting the call inside the condition so that it doesn't need to be written twice in the code:

while( confirm("Do you want to calculate...") )
{
...
}

[edited by: Bernard_Marx at 12:54 pm (utc) on June 21, 2007]

java_junkie

1:06 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



it's still responding undefined interest

Bernard Marx

1:23 pm on Jun 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Look at this line:

interest = [blue]calculateInterest[/blue](amountInPounds); 

Now look at the function, calculateInterest:

function calculateInterest(numberOfPounds) 
{

};

Your script is possibly feeling a little neglected ;)

java_junkie

11:24 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



Finally I have arrived at it but God it took ages

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

java_junkie

11:29 pm on Jun 21, 2007 (gmt 0)

10+ Year Member



With the correct code now is it possible to separate out successive lines of output, and instead of using document.write('<BR><BR><BR>') can I replace it with a statement like this leaveGap(2)

Greven

4:48 am on Jun 22, 2007 (gmt 0)

10+ Year Member



documement.write is a cludge, you may like using dom functions better, such as:

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]);

java_junkie

7:00 am on Jun 22, 2007 (gmt 0)

10+ Year Member



what if I want to write a function called leaveGap(2) instead of using document.write('<BR><BR><BR>')