/    Sign up×
Community /Pin to ProfileBookmark

NaN error for unknown reason involving forms

I want to do this fuel economy checker thing, however it always gives me a NaN error in the alert box?

[code]
<html>
<head><title>Fuel Economy Checker</title></head>
<body>
<script language=”javascript”>

var kms=0;
var dollars=0;

document.write(“<form><table><tr><td>Kilometres Travelled</td><td><input name=”+kms+”></td>”+
“<tr><td>Dollars of Fuel Consumed</td><td><input name=”+dollars+”></td></table><br>”+
“<input type=’button’ name=’submit’ value=’Check it’ onClick=’checkit()’>”);

function checkit()
{
var answer=0;
dollars = dollars*10
answer = dollars/kms;
alert(answer+” cents per Km”);
}

</script>
</body>
</html>
[/code]

What the hell? It should work, shouldn’t it? I defined the variables as number with the =0 on the end didn’t I?

Cheers
hamstar

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@KorOct 17.2004 — Don't repeat the quote type inside a string unless you concatenate a string with a variable than another string.

use

document.write([color=red]'[/color]<tag attribute="value">[color=red]'[/color])

or the "quoting as character" backslash notation

document.write("<tag attribute=[color=red][/color]"value[color=red][/color]">")
Copy linkTweet thisAlerts:
@KorOct 17.2004 — Why not use DOM to create elements? But, in fact... do you really need to create that elements instead of writting them as HTML?
Copy linkTweet thisAlerts:
@Mr_JOct 17.2004 — Please try the following


[SIZE=1]

<html>

<head>

<title>Fuel Economy Checker</title></head>

<body>

<script language="javascript">



var kms=0;

var dollars=0;



document.write("<form name="f1"><table><tr><td>Kilometres Travelled</td><td><input name="kms"></td>"+

"<tr><td>Dollars of Fuel Consumed</td><td><input name="dollars"></td></table><br>"+

"<input type='button' name='submit' value='Check it' onClick='checkit()'>");



function checkit(){

var answer=0;

kms=document["f1"]["kms"].value;

dollars=document["f1"]["dollars"].value;



dollars = dollars*10;

answer =dollars/kms;

alert(answer+" cents per Km");

}



</script>

</body>

</html>



[/SIZE]
Copy linkTweet thisAlerts:
@JPnycOct 17.2004 — parseInt the value from the txt fields.
Copy linkTweet thisAlerts:
@Warren86Oct 17.2004 — <HTML>

<Head>

<Script Language=JavaScript>

function getResult(isForm){

Kms = parseFloat(isForm.kms.value);
Kms = Kms.toFixed(2);
isForm.kms.value = Kms;
cost = parseFloat(isForm.dollars.value);
cost = cost.toFixed(2);
isForm.dollars.value = cost;
isResult = Kms/cost;
isResult = isResult.toFixed(1);
isForm.result.value = isResult
}


</Script>

</Head>

<Body>

<form name=Form1>

<table>

<tr>

<td>Kilometres Travelled</td>

<td><input type=text name='kms' size=6 onClick=reset()></td>

</tr>

<tr>

<td>Dollars of Fuel Consumed</td>

<td><input type=text name='dollars' size=6></td>

</tr>

<tr>

<td>Km per Dollar</td>

<td><input type=text name='result' size=6></td>

</tr>

</table>

<br>

<input type='button'value='Check it' onClick="getResult(Form1)">

</form>

<script> Form1.kms.focus()</script>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@hamstarauthorOct 17.2004 — Ok, got it working using:
<i>
</i>kms=document["f1"]["kms"].value;
dollars=document["f1"]["dollars"].value;


But I wanna round it off in the alert box and it wont let me do .toFixed(2). Says "Object doesn't support property or method"
Copy linkTweet thisAlerts:
@Mr_JOct 17.2004 — Try changing the line

answer = dollars/kms;

to

answer =[B]([/B]dollars/kms[B]).toFixed(2)[/B];

or the alert

alert(answer+" cents per Km");

to

alert(answer[B].toFixed(2)[/B]+" cents per Km");
Copy linkTweet thisAlerts:
@hamstarauthorOct 17.2004 — Yeah I tried that, but it doesn't like it...
Copy linkTweet thisAlerts:
@Mr_JOct 18.2004 — What browser are you using?

I tried it in IE6, Mozilla, and Firefox and it worked in all 3.


[SIZE=1]

<html>

<head>

<title>Fuel Economy Checker</title></head>

<body>

<script language="javascript">



var kms=0;

var dollars=0;



document.write("<form name="f1"><table><tr><td>Kilometres Travelled</td><td><input name="kms"></td>"+

"<tr><td>Dollars of Fuel Consumed</td><td><input name="dollars"></td></table><br>"+

"<input type='button' name='submit' value='Check it' onClick='checkit()'>");



function checkit(){

var answer=0;

kms=document["f1"]["kms"].value;

dollars=document["f1"]["dollars"].value;



dollars = dollars*10;

answer =(dollars/kms)[B].toFixed(2)[/B];

alert(answer+" cents per Km");

}



</script>

</body>

</html>









[/SIZE]
Copy linkTweet thisAlerts:
@hamstarauthorOct 28.2004 — I restarted my computer and it started working fine!

Thanks for all your help!
×

Success!

Help @hamstar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...