Connecting Tech Pros Worldwide Forums | Help | Site Map

best practice if then else

Bryan Hepworth
Guest
 
Posts: n/a
#1: May 21 '06
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


Ian Collins
Guest
 
Posts: n/a
#2: May 21 '06

re: best practice if then else


Bryan Hepworth wrote:[color=blue]
> 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.
>[/color]
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.

--
Ian Collins.
Bryan Hepworth
Guest
 
Posts: n/a
#3: May 21 '06

re: best practice if then else


[color=blue]
> 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.
>[/color]

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
#4: May 21 '06

re: best practice if then else


Bryan Hepworth wrote:[color=blue][color=green]
>>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.
>>[/color]
>
>
> Tip-Top Ian, Just what I wanted! Now to figure it out and code it up. Thanks
> for the swift reply much appreciated.
>[/color]
No problem, pop back if you have any problems.

--
Ian Collins.
Bryan Hepworth
Guest
 
Posts: n/a
#5: May 21 '06

re: best practice if then else


> No problem, pop back if you have any problems.[color=blue]
>
> --
> Ian Collins.[/color]

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


Ian Collins
Guest
 
Posts: n/a
#6: May 21 '06

re: best practice if then else


Bryan Hepworth wrote:[color=blue][color=green]
>>No problem, pop back if you have any problems.
>>
>>--
>>Ian Collins.[/color]
>
>
> 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.
>[/color]
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.
Bryan Hepworth
Guest
 
Posts: n/a
#7: May 21 '06

re: best practice if then else


> switch( funacetemp )[color=blue]
> {
> case 900:
>
> calulateFor900();
> break;
>
> case 1200:
>
> calulateFor900();
> break;
>
> ...
>
> default:
>
> //some warning
> }
>
> each calulateForN function can use the same form.
>[/color]

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


Ian Collins
Guest
 
Posts: n/a
#8: May 21 '06

re: best practice if then else


Bryan Hepworth wrote:[color=blue][color=green]
>>switch( funacetemp )
>>{
>> case 900:
>>
>> calulateFor900();
>> break;
>>
>> case 1200:
>>
>> calulateFor900();
>> break;
>>
>>...
>>
>> default:
>>
>> //some warning
>>}
>>
>>each calulateForN function can use the same form.
>>[/color]
>
>
> 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[/color]
missing :


--
Ian Collins.
Dr John Stockton
Guest
 
Posts: n/a
#9: May 21 '06

re: best practice if then else


JRS: In article <446f8c41$0$690$fa0fcedb@news.zen.co.uk>, dated Sat, 20
May 2006 22:38:09 remote, seen in news:comp.lang.javascript, Bryan
Hepworth <bryanatredfedora.co.uk@?.?> posted :[color=blue]
>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?[/color]

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

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Bryan Hepworth
Guest
 
Posts: n/a
#10: May 22 '06

re: best practice if then else


Lee wrote:[color=blue]
> Bryan Hepworth said:
>[color=green]
>>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.[/color]
>
>
> Here's an example using a 3-dimensional array as a lookup table:
>[/color]

Lee

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

Bryan
Closed Thread