Velocity Reviews > best practice if then else

# best practice if then else

Bryan Hepworth
Guest
Posts: n/a

 05-20-2006
Hi Everyone

I'm wondering what the best practice is for a particular task I'm trying to
accomplish. I'm using two sets of radio buttons for a user to select values
from. These values then go into a couple of tables to show some heat loss
calculations depending on the values chosen I want the tables to have the
relevant values filled in. Is the best way to accomplish this a long list of
if then else type statements or is there a better way of doing this?

If anyone could point me in the right direction I'd be really grateful.

Thanks

Bryan

Ian Collins
Guest
Posts: n/a

 05-20-2006
Bryan Hepworth wrote:
> Hi Everyone
>
> I'm wondering what the best practice is for a particular task I'm trying to
> accomplish. I'm using two sets of radio buttons for a user to select values
> from. These values then go into a couple of tables to show some heat loss
> calculations depending on the values chosen I want the tables to have the
> relevant values filled in. Is the best way to accomplish this a long list of
> if then else type statements or is there a better way of doing this?
>
>
> If anyone could point me in the right direction I'd be really grateful.
>

If there are more than two conditions and these can be expressed as
constant expressions, use a switch statement. It is less error prone
when you expand and easier to read.

Looking at your example, you have an ideal case with the values passed

--
Ian Collins.

Bryan Hepworth
Guest
Posts: n/a

 05-20-2006

> If there are more than two conditions and these can be expressed as
> constant expressions, use a switch statement. It is less error prone
> when you expand and easier to read.
>
> Looking at your example, you have an ideal case with the values passed
>

Tip-Top Ian, Just what I wanted! Now to figure it out and code it up. Thanks
for the swift reply much appreciated.

Bryan

Ian Collins
Guest
Posts: n/a

 05-21-2006
Bryan Hepworth wrote:
>>If there are more than two conditions and these can be expressed as
>>constant expressions, use a switch statement. It is less error prone
>>when you expand and easier to read.
>>
>>Looking at your example, you have an ideal case with the values passed
>>

>
>
> Tip-Top Ian, Just what I wanted! Now to figure it out and code it up. Thanks
> for the swift reply much appreciated.
>

No problem, pop back if you have any problems.

--
Ian Collins.

Bryan Hepworth
Guest
Posts: n/a

 05-21-2006
> No problem, pop back if you have any problems.
>
> --
> Ian Collins.

Ian

I'm struggling with the switch format - I couldn't see how I could get it to
check both values and then do the cell changes. I thought I'd illustrate
this with an if elseif, but frustratingly I can't get that to work either!
I've uploaded the changes to the script so far and commented out the if

I'm doing something blindingly obvious wrong but can't suss it out - new to
javascript if you hadn't guessed and this is my first attempt.

Thanks very much.

Bryan

Ian Collins
Guest
Posts: n/a

 05-21-2006
Bryan Hepworth wrote:
>>No problem, pop back if you have any problems.
>>
>>--
>>Ian Collins.

>
>
> Ian
>
> I'm struggling with the switch format - I couldn't see how I could get it to
> check both values and then do the cell changes. I thought I'd illustrate
> this with an if elseif, but frustratingly I can't get that to work either!
> I've uploaded the changes to the script so far and commented out the if
>
> I'm doing something blindingly obvious wrong but can't suss it out - new to
> javascript if you hadn't guessed and this is my first attempt.
>

You are trying to do too much in one go. Break the problem down into
bite sized chunks, something like

switch( funacetemp )
{
case 900:

calulateFor900();
break;

case 1200:

calulateFor900();
break;

....

default:

//some warning
}

each calulateForN function can use the same form.

--
Ian Collins.

Dr John Stockton
Guest
Posts: n/a

 05-21-2006
JRS: In article <446f8c41\$0\$690\$(E-Mail Removed)>, dated Sat, 20
May 2006 22:38:09 remote, seen in news:comp.lang.javascript, Bryan
Hepworth <bryanatredfedora.co.uk@?.?> posted :
>Hi Everyone
>
>I'm wondering what the best practice is for a particular task I'm trying to
>accomplish. I'm using two sets of radio buttons for a user to select values
>from. These values then go into a couple of tables to show some heat loss
>calculations depending on the values chosen I want the tables to have the
>relevant values filled in. Is the best way to accomplish this a long list of
>if then else type statements or is there a better way of doing this?

The following scans a set of radiobuttons identified by Rbtn and returns
the value of the element of the array Arr corresponding to the selected
button.

function RadBtns(Rbtn, Arr) { var Q, J=0
while (Q=Rbtn[J]) { if (Q.checked) return Arr[J] ; J++ } }

The array can be supplied as a literal parameter (for a unique set of
buttons) or as a variable. The contents of the array can be of any
type, though it's likely that they will be similar.

From <URL:http://www.merlyn.demon.co.uk/estr-bcp.htm#33>, in which the
buttons select which Easter function is to be tested (years in error, if
any, would be marked X) :-

function BCPtest(F) { var St, A, B, Y, OK, OKA = ["X", "."], E = 0
with (F) { var Y1 = GetNum(y1), Y2 = GetNum(y2),
BCPn = RadBtns(RBn, [BCPEaster1, BCPEaster2, BCPEaster3]) }
St = FuncName(BCPn) + " : X marks errors\n"
for (Y=Y1 ; Y<=Y2; Y++) { if (Y%10==0) St += "\n"
A = BCPn(Y)
B = EGREaster(Y) ; B = new Date(Date.UTC(Y, B.M-1, B.D))
OK = +A==+B ; if (!OK) E++
St += Y + OKA[+OK] + " " }
document.write("<pre>", St, "\n Errors ", E, "<\pre>") }

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

Bryan Hepworth
Guest
Posts: n/a

 05-21-2006
> switch( funacetemp )
> {
> case 900:
>
> calulateFor900();
> break;
>
> case 1200:
>
> calulateFor900();
> break;
>
> ...
>
> default:
>
> //some warning
> }
>
> each calulateForN function can use the same form.
>

Ian

Whereabouts would this code go - I'm getting totally confused. I was trying
to get it to execute from the calculate button like so:

function docalculation(furnacetemp)
switch (furnacetemp)
{
case 900:
break;
case 1050:
break;
case 1150:
case 1250:
break;
default
}

But I'm getting errors as soon as I load the page.

Thanks

Bryan

Lee
Guest
Posts: n/a

 05-21-2006
Bryan Hepworth said:
>
>Hi Everyone
>
>I'm wondering what the best practice is for a particular task I'm trying to
>accomplish. I'm using two sets of radio buttons for a user to select values
>from. These values then go into a couple of tables to show some heat loss
>calculations depending on the values chosen I want the tables to have the
>relevant values filled in. Is the best way to accomplish this a long list of
>if then else type statements or is there a better way of doing this?
>
>
>If anyone could point me in the right direction I'd be really grateful.

If they're really calculations, then take your two inputs and perform
the calculations. No need for if's or switches.

--

Lee
Guest
Posts: n/a

 05-21-2006
Bryan Hepworth said:
>
>Hi Everyone
>
>I'm wondering what the best practice is for a particular task I'm trying to
>accomplish. I'm using two sets of radio buttons for a user to select values
>from. These values then go into a couple of tables to show some heat loss
>calculations depending on the values chosen I want the tables to have the
>relevant values filled in. Is the best way to accomplish this a long list of
>if then else type statements or is there a better way of doing this?
>
>
>If anyone could point me in the right direction I'd be really grateful.

Here's an example using a 3-dimensional array as a lookup table:

<script type="text/javascript">
var globalFurnaceTemp=0;
var globalFurnaceLining=0;
var table=[]
table[900]=[];
table[1050]=[];
table[1150]=[];
table[1250]=[];
table[900][200]=[72,64,20,64,20];
table[900][230]=[110,90,28,64,60];
table[900][343]=[99,84,24,64,53];
table[900][457]=[91,79,22,64,45]
table[1050][200]=[86,75,20,75,20];
table[1050][230]=[124,104,25,75,56];
table[1050][343]=[112,96,22,75,49];
table[1050][457]=[103,90,24,75,41];
table[1150][200]=[96,84,20,84,20];
table[1150][230]=[134,114,24,84,54];
table[1150][343]=[121,105,21,84,45];
table[1150][457]=[111,98,20,93,20];
table[1250][200]=[107,93,20,93,20];
table[1250][230]=[144,124,22,93,51];
table[1250][343]=[130,144,19,93,42];
table[1250][457]=[119,106,17,93,33];

function checktemperature(furnacetemp)
{
globalFurnaceTemp=parseInt(furnacetemp);
document.getElementById('myCurrentFurnace').rows[1].cells[0].innerHTML=furnacetemp
// Align Centre - note spelling! - Cell Temperature current furnace
document.getElementById('myCurrentFurnace').rows[1].cells[0].align="center"
document.getElementById('myProposedNewFurnace1').r ows[1].cells[0].innerHTML=furnacetemp
// Align Centre - note spelling! - Cell Temperature additional 50mm module
document.getElementById('myProposedNewFurnace1').r ows[1].cells[0].align="center"
document.getElementById('myProposedNewFurnace2').r ows[1].cells[0].innerHTML=furnacetemp
// Align Centre - note spelling! - Cell Temperature 250mm module conversion
document.getElementById('myProposedNewFurnace2').r ows[1].cells[0].align="center"
}
function checklining(furnacelining)
{
globalFurnaceLining=parseInt(furnacelining);
document.getElementById('myCurrentFurnace').rows[1].cells[1].innerHTML=furnacelining
// Align Centre - note spelling! - Cell Furnace Lining current furnace
document.getElementById('myCurrentFurnace').rows[1].cells[1].align="center"
document.getElementById('myProposedNewFurnace1').r ows[1].cells[1].innerHTML=furnacelining
// Align Centre - note spelling! - Cell Furnace Lining additional 50mm module
document.getElementById('myProposedNewFurnace1').r ows[1].cells[1].align="center"
document.getElementById('myProposedNewFurnace2').r ows[1].cells[1].innerHTML=furnacelining
// Align Centre - note spelling! - Cell Furnace Lining 250mm module conversion
document.getElementById('myProposedNewFurnace2').r ows[1].cells[1].align="center"
// Turn the Current Furnace Cold Face Temperature Cell Red
document.getElementById("myCurrentFurnace").rows[1].cells[2].style.background =
"#FF9900"
// Turn the 50mm Module Addition % Saving Cell Red
document.getElementById("myProposedNewFurnace1").r ows[1].cells[3].style.background
= "#FF9900"
// Turn the 250mm Module Conversion % Saving Cell Red
document.getElementById("myProposedNewFurnace2").r ows[1].cells[3].style.background
= "#FF9900"
}

function docalculation()
{
if(table[globalFurnaceTemp][globalFurnaceLining]) {
var lookup=table[globalFurnaceTemp][globalFurnaceLining];
var current=document.getElementById('myCurrentFurnace' );
var prop1=document.getElementById('myProposedNewFurnac e1');
var prop2=document.getElementById('myProposedNewFurnac e2');

current.rows[1].cells[2].innerHTML=lookup[0];
prop1.rows[1].cells[2].innerHTML=lookup[1];
prop1.rows[1].cells[3].innerHTML=lookup[2];
prop2.rows[1].cells[2].innerHTML=lookup[3];
prop2.rows[1].cells[3].innerHTML=lookup[4];

current.rows[1].cells[2].align="center"
prop1.rows[1].cells[2].align="center"
prop1.rows[1].cells[3].align="center"
prop2.rows[1].cells[2].align="center"
prop2.rows[1].cells[3].align="center"
}
}

</script>

--