Re: Switch() Statement Not Working
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Switch() Statement Not Working

From: web.dev <web.dev.cs@gmail.com>
Date: Tue Jan 31 2006 - 21:04:30 CET

rdavis7408@gmail.com wrote:
> <script language="JavaScript">

The language attribute is deprecated, use the type attribute instead:

<script type = "text/javascript">

> b=this.form.ppg.value

This statement is error-prone. Instead give your form a name and
access its elements in the following fashion:

var b = document.forms["formName"].elements["ppg"].value;

> switch(b)
> {
> Case >=1.25 && <=1.30:
> this.form.dlnt.value=.01;
> Case >=1.31 && <=1.369:
> this.form.dlnt.value=.02;
> Case >=1.37 && <=1.429:
> this.form.dlnt.value=.03;
> default:
> this.form.dlnt.value=.99
>
> }

1. It is not 'Case', but should be 'case' with a lowercase C.
2. You should have a 'break' after one or more cases. And a 'break'
after the default case.
3. The switch construct does not deal with a range of values. You
should instead use if...else statements instead.

var dintVal = .99;

if(b >= 1.25 && b <= 1.3)
{
   dintVal = .01
}
else if(b >= 1.31 && b <= 1.369)
{
   dintVal = .02
}
else if(b >= 1.37 && b <= 1.429)
{
   dintVal = .03;
}

document.forms["formName"].elements["dInt"].value = dintVal;
Received on Tue Feb 7 21:29:13 2006