best practice if then else

Discussion in 'Javascript' started by Bryan Hepworth, May 20, 2006.

  1. 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
     
    Bryan Hepworth, May 20, 2006
    #1
    1. Advertising

  2. Bryan Hepworth

    Ian Collins Guest

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

    --
    Ian Collins.
     
    Ian Collins, May 20, 2006
    #2
    1. Advertising


  3. > 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
     
    Bryan Hepworth, May 20, 2006
    #3
  4. Bryan Hepworth

    Ian Collins Guest

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

    No problem, pop back if you have any problems.

    --
    Ian Collins.
     
    Ian Collins, May 21, 2006
    #4
  5. > 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
    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
     
    Bryan Hepworth, May 21, 2006
    #5
  6. Bryan Hepworth

    Ian Collins Guest

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

    --
    Ian Collins.
     
    Ian Collins, May 21, 2006
    #6
  7. JRS: In article <446f8c41$0$690$>, 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>") }

    --
    © 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.
     
    Dr John Stockton, May 21, 2006
    #7
  8. > 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
     
    Bryan Hepworth, May 21, 2006
    #8
  9. Bryan Hepworth

    Lee Guest

    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.


    --
     
    Lee, May 21, 2006
    #9
  10. Bryan Hepworth

    Lee Guest

    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>


    --
     
    Lee, May 21, 2006
    #10
  11. Bryan Hepworth

    Ian Collins Guest

    Bryan Hepworth wrote:
    >>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

    missing :


    --
    Ian Collins.
     
    Ian Collins, May 21, 2006
    #11
  12. Lee wrote:
    > 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:
    >


    Lee

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

    Bryan
     
    Bryan Hepworth, May 22, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    25
    Views:
    702
    MonkeeSage
    Oct 1, 2006
  2. kj
    Replies:
    15
    Views:
    605
    Lawrence D'Oliveiro
    May 23, 2009
  3. Replies:
    0
    Views:
    336
  4. Replies:
    15
    Views:
    247
  5. oldyork90
    Replies:
    1
    Views:
    177
    Jeremy J Starcher
    Sep 10, 2008
Loading...

Share This Page