|
From: rdavis7408 on 31 Jan 2006 14:09 Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to pull a value based on the price per gallon. For example if the price of fuel is 2.44 per gallon and the enter that they get 5.9 miles per gallon the cost of that mile is $.41. Then based on the cost per gallon of 2.44 we might pay them another $.20 per gallon based off of the numbers in the switch statement. The calculation worked until I added the switch statement. Why is that? Can anyone help? Thanks ahead of time. Below is the script: <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <link href="fsc.css" rel="stylesheet" type="text/css"> </head> <body> <script language="JavaScript"> function FuelCalculator(f) { var A; A= Math.round((f.ppg.value/f.mpg.value)*100); f.cpg.value = "$"+(A/100); } var b; b=this.form.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 } </script> <form action=""> <table width="351"> <tr> <td width="213"> Cents per gallon:</td> <td width="60" > <input name="ppg" type="text" style="text-align:center" value="0" size="10"> </td> </tr> <tr> <td>Miles per gallon:</td> <td> <input name="mpg" type="text" value="0" size="10" style="text-align:center"> </td> </tr> <tr> <td>Fuel Cost Per Mile: </td> <td > <input name="cpg" type= "text" disabled="true" value="0" size="10" style="text-align:center"> </td> <tr> <td>Dillon Fuel Surcharge Payment: </td> <td> <input name="dlnt" type="text" disabled="true" value="0" size="10" style="text-align:center"> </td> <tr> <td colspan="2" ><input name="button" type="button" onClick="FuelCalculator(this.form);" value="Calclulate"> </td> </tr> <td><input name="FSC" type="button" onClick="switch(b)" value="FSC"></td> </table> </form> </body> </html>
From: Evertjan. on 31 Jan 2006 14:14 wrote on 31 jan 2006 in comp.lang.javascript: > switch(b) > { > Case >=1.25 && <=1.30: > this.form.dlnt.value=.01; > It is all in the name: case is lowercase! -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Evertjan. on 31 Jan 2006 14:15 Evertjan. wrote on 31 jan 2006 in comp.lang.javascript: > wrote on 31 jan 2006 in comp.lang.javascript: > >> switch(b) >> { >> Case >=1.25 && <=1.30: >> this.form.dlnt.value=.01; >> > > It is all in the name: > > case is lowercase! > and give me a break: you will have to use break; -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: web.dev on 31 Jan 2006 15:04 rdavis7408(a)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;
From: Lasse Reichstein Nielsen on 31 Jan 2006 20:12
rdavis7408(a)gmail.com writes: > The calculation worked until I added the switch statement. Why is that? > 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 > > } You are guessing blindly. That rarely works in programming, since computers are notoriously bad at guessing what you really meant. The syntax of a case statement is: case <expression>: <statement> Example: switch(num){ case 1: // something for 1 break; case 2: case 3: // something for 2 or 3 break; default: // ... } In your case, you want to match intervals, not values, so a switch statement isn't the immediate choice. Instead use a sequence of if-statements: if (b >= 1.25 && b <= 1.30) { this.form.dlnt.value=.01; } else if (b > 1.30 && b < 1.37) { ... } else if (b >= 1.37 && b < 1.43) { ... } else { ... } You can get the same effect with a switch, but it's not really smarter, and it's much less readable. switch(true) { case (b >= 1.25 && b <= 1.30) : this.form.dlnt.value=.01; break; case (b > 1.30 && b < 1.37) : ... break; ... } /L -- Lasse Reichstein Nielsen - lrn(a)hotpop.com DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.' |