best practice if then else

B

Bryan Hepworth

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?

http://www.redfedora.co.uk/jscriptradio.html

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

Thanks

Bryan
 
I

Ian Collins

Bryan 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?

http://www.redfedora.co.uk/jscriptradio.html

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
to your checkxxx functions.
 
B

Bryan Hepworth

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
to your checkxxx functions.

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

Bryan
 
I

Ian Collins

Bryan said:
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.
 
B

Bryan Hepworth

No problem, pop back if you have any problems.
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
elseif. http://www.redfedora.co.uk/jscriptradio.html

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
 
I

Ian Collins

Bryan said:
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
elseif. http://www.redfedora.co.uk/jscriptradio.html

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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 20
May 2006 22:38:09 remote, seen in 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?

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>") }
 
B

Bryan Hepworth

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:
alert("900");
break;
case 1050:
alert("1050");
break;
case 1150:
alert("1150");
case 1250:
alert("1250");
break;
default
alert("got this far");
}

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

Thanks

Bryan
 
L

Lee

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?

http://www.redfedora.co.uk/jscriptradio.html

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.


--
 
L

Lee

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?

http://www.redfedora.co.uk/jscriptradio.html

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').rows[1].cells[0].innerHTML=furnacetemp
// Align Centre - note spelling! - Cell Temperature additional 50mm module
document.getElementById('myProposedNewFurnace1').rows[1].cells[0].align="center"
document.getElementById('myProposedNewFurnace2').rows[1].cells[0].innerHTML=furnacetemp
// Align Centre - note spelling! - Cell Temperature 250mm module conversion
document.getElementById('myProposedNewFurnace2').rows[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').rows[1].cells[1].innerHTML=furnacelining
// Align Centre - note spelling! - Cell Furnace Lining additional 50mm module
document.getElementById('myProposedNewFurnace1').rows[1].cells[1].align="center"
document.getElementById('myProposedNewFurnace2').rows[1].cells[1].innerHTML=furnacelining
// Align Centre - note spelling! - Cell Furnace Lining 250mm module conversion
document.getElementById('myProposedNewFurnace2').rows[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").rows[1].cells[3].style.background
= "#FF9900"
// Turn the 250mm Module Conversion % Saving Cell Red
document.getElementById("myProposedNewFurnace2").rows[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('myProposedNewFurnace1');
var prop2=document.getElementById('myProposedNewFurnace2');

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>


--
 
I

Ian Collins

Bryan said:
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:
alert("900");
break;
case 1050:
alert("1050");
break;
case 1150:
alert("1150");
case 1250:
alert("1250");
break;
default
missing :
 
B

Bryan Hepworth

Lee said:
Bryan Hepworth said:



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

Lee

Thanks for posting - I'm really appreciative, must buy you a beer or two!

Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top