/    Sign up×
Community /Pin to ProfileBookmark

Help needed with Nan and Infinity errors, also identifying min value

Hi,

I am new to Javascript and am having a few problems with what are probably very simple issues.

In the attached code how do I prevent “NaN” and “Infinity” error messages in the result1-3 fields.

I have had a go at the isNAN() etc functions but I just cant seem to get the syntax right.

How would I restrict the results to 3 decimal places?

I also want to evalaute the three results and populate the lowest field with a text response indicating which of the results has the lowest value, ie “Result 1 is the lowest value”.

Thanks for any assistance you can offer,

Cheers

Iain

[CODE]
<html>

<head>

<script language=”javascript” type=”text/javascript”>
<!– –>

function cost1()
{
document.calc.result1.value=document.calc.num1.value*100/document.calc.num2.value;
}

function cost2()
{
document.calc.result2.value=document.calc.num3.value*100/document.calc.num4.value;
}

function cost3()
{
document.calc.result3.value=document.calc.num5.value*100/document.calc.num6.value;
}

</script>

</head>

<body>

<form name=”calc”>

<table>

<tr>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num1″
size=”10″ style=”text-align: center” />
</td>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num2″
size=”10″ style=”text-align: center” />
</td>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”result1″ size=”14″ style=”text-align: right” />
</td>

</tr>

<tr>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num3″
size=”10″ style=”text-align: center” />
</td>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num4″ size=”10″
style=”text-align: center” />
</td>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”result2″
size=”14″ style=”text-align: right” />
</td>

</tr>

<tr>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num5″
size=”10″ style=”text-align: center” />
</td>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”num6″ size=”10″
style=”text-align: center” />
</td>

<td align=”center”>
<input type=”text”
value=”0″
onfocus=”if(this.value == this.defaultValue) this.value = ””
name=”result3″
size=”14″ style=”text-align: right” />
</td>

</tr>

<tr>

<td align=”center”>
<input type=”button” style=”font-size: 12pt; color: #oooooo” value=”Calculate” onClick=”cost1();cost2();cost3()” />
</td>

</tr>

<tr>
<td align=”center”>
<input type=”text” name=”lowest” size=”14″ style=”text-align: center” />
</td>
</tr>

</table>

</body>

</html>
[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@tirnaFeb 18.2011 — Hi,

In the attached code how do I prevent "NaN" and "Infinity" error messages in the result1-3 fields.

How would I restrict the results to 3 decimal places?
[/quote]


Use isNaN() or the Number object to validate the user inputs.

Validate the data before doing any division by checking first if the denominator = 0 to stop "inifinity" or "divide by 0" errors.

Use toFixed() to specify the numger of decimal places.
Copy linkTweet thisAlerts:
@ijd65authorFeb 23.2011 — Thanks
×

Success!

Help @ijd65 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...