Log in

View Full Version : undefined!! help javascript



vbean
03-26-2009, 06:36 PM
how to i make it not say undefined as an answer?

<HTML>
<HEAD>
<TITLE> BAD CREDIT </TITLE>
<SCRIPT type="text/javascript">
function monthlyRate () {
var price =
parseFloat(document.getElementById("price").value);
var interest =
parseFloat(document.getElementById("interest").value);
var pay=
parseFloat(document.getElementById("pay").value);
if (isNaN(price) || isNaN(interest) || isNaN(pay)) {
return;
}


price.toFixed(2)
var monthrate = interest / 100 ;
monthrate = monthrate + 1 ;
monthrate = ((Math.pow(1 + interest , 1/12)) - 1);

var monthly = monthrate;
var bill = "+ price +" ;
var month = 0;


while ( bill >= pay ) {
bill = price - pay ;
bill = bill * (1 + monthly);
price = bill ;

}
if ( bill < pay ) {
var totalamount = pay * month + bill ;

}

document.getElementById("amount").value = totalamount;



}


</SCRIPT>
</HEAD>
<BODY>
Enter the purchase price for the item you are interested in buying: <input onchange=monthlyRate() size=13 type=text id=price /><br/>
Enter the annual interest rate on your credit card: <input onchange=monthlyRate() size=13 type=text id=interest /><br/>
Enter the amount of money you plan to pay towards you credit card balance each month: <input onchange=monthlyRate() size=13 type=text id=pay /><br/>
The amount you actually spent is: <input size=5 type=text id=amount /><br/>




</BODY>
</HTML>

Nile
03-26-2009, 06:38 PM
Its saying undefined because here:


if ( bill < pay ) {
var totalamount = pay * month + bill ;

}


It is getting defined, but obviously bill isn't less than pay, so it's not being defined.

vbean
03-26-2009, 06:41 PM
how would you suggest i fix this?

Nile
03-26-2009, 06:44 PM
Well, first of all. I suggest learning some more basic Javascript. Like Javascript Concatenation (http://jennifermadden.com/javascript/concatenation.html).

I don't think:


var bill = "+ price +" ;
Is valid.

vbean
03-26-2009, 06:51 PM
ok well if i take "+price+" away, i don't get anything at all as an answer. just a blank textbox... so the computer doesn't know what goes under var bill ..

Nile
03-26-2009, 06:53 PM
Do:


var bill = price;

vbean
03-26-2009, 06:58 PM
ok i did and got an alert, which i clicked on "debug" ... and got..

Nile
03-26-2009, 06:59 PM
If bill isn't greater than pay, what should the value of totalamount be?

vbean
03-26-2009, 07:02 PM
a negative?

vbean
03-26-2009, 07:14 PM
im not sure what you mean. im sorry.

Nile
03-26-2009, 07:16 PM
If you don't want it to be undefined, you have to give it a value if bill isn't greater than pay. Did you make this code?