Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    were should } be added?

    Hi,
    This code is part of a function I known in the if im missing a } but im not sure were to add it ?


    Code:
      if (cbiindex > "97.5") {
         cbitxt = langChandler[4];
        cbiimg= "/Fire32";
    
    } else if (cbiindex >="90") {
         cbitxt = langChandler[3];
        cbiimg= "/Fire31";
    
    } else if (cbiindex >= "75") {
         cbitxt = langChandler[2];
        cbiimg= "/Fire25";
    
    } else if (cbiindex >= "50") {
        cbitxt = langChandler[1];
        cbiimg= "/Fire16";
    
    } else {
    
    if (cbiindex< "50") {
         cbitxt = langChandler[0];
        cbiimg= "/Fire0";
    
    }
      
      cbitxt = langChandlerMsg + ': ' + cbitxt;
      cbi_image = '<img src="' + imagedir2 + cbiimg + '.gif" alt="' + cbitxt + '" title="' + cbitxt + '" />';
      cbiinfo = new Array ( cbiindex, cbitxt, cbi_image );
      return cbiinfo;
    }
    thanks Beau

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
      if (cbiindex > "97.5") {
         cbitxt = langChandler[4];
        cbiimg= "/Fire32";
    
    } else if (cbiindex >="90") {
         cbitxt = langChandler[3];
        cbiimg= "/Fire31";
    
    } else if (cbiindex >= "75") {
         cbitxt = langChandler[2];
        cbiimg= "/Fire25";
    
    } else if (cbiindex >= "50") {
        cbitxt = langChandler[1];
        cbiimg= "/Fire16";
    
    } else if (cbiindex< "50") {
         cbitxt = langChandler[0];
        cbiimg= "/Fire0";
    
    }
      
      cbitxt = langChandlerMsg + ': ' + cbitxt;
      cbi_image = '<img src="' + imagedir2 + cbiimg + '.gif" alt="' + cbitxt + '" title="' + cbitxt + '" />';
      cbiinfo = new Array ( cbiindex, cbitxt, cbi_image );
      return cbiinfo;
    }

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Give me the complete function of this code, not just a of it.
    PHP Code:
    if (cbiindex "97.5") {
        
    cbitxt langChandler[4];
        
    cbiimg "/Fire32";

    } else if (
    cbiindex >= "90") {
        
    cbitxt langChandler[3];
        
    cbiimg "/Fire31";

    } else if (
    cbiindex >= "75") {
        
    cbitxt langChandler[2];
        
    cbiimg "/Fire25";

    } else if (
    cbiindex >= "50") {
        
    cbitxt langChandler[1];
        
    cbiimg "/Fire16";

    } else {

        if (
    cbiindex "50") {
            
    cbitxt langChandler[0];
            
    cbiimg "/Fire0";

        }

        
    cbitxt langChandlerMsg ': ' cbitxt;
        
    cbi_image '<img src="' imagedir2 cbiimg '.gif" alt="' cbitxt '" title="' cbitxt '" />';
        
    cbiinfo = new Array(cbiindexcbitxtcbi_image);
        return 
    cbiinfo;


  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    The quotes in the numbers should be removed.

    Code:
    if (cbiindex > 97.5) {

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Without the complete code for the function, I'm guessing the last 4 lines should be outside the "else" block.
    Code:
    function whatever() {
        if (cbiindex > 97.5) { 
            cbitxt = langChandler[4]; 
            cbiimg = "/Fire32"; 
    
        } else if (cbiindex >= 90) { 
            cbitxt = langChandler[3]; 
            cbiimg = "/Fire31"; 
    
        } else if (cbiindex >= 75) { 
            cbitxt = langChandler[2]; 
            cbiimg = "/Fire25"; 
    
        } else if (cbiindex >= 50) { 
            cbitxt = langChandler[1]; 
            cbiimg = "/Fire16"; 
    
        } else  { 
            cbitxt = langChandler[0]; 
            cbiimg = "/Fire0"; 
        }  
    
        cbitxt = langChandlerMsg + ': ' + cbitxt; 
        cbi_image = '<img src="' + imagedir2 + cbiimg + '.gif" alt="' + cbitxt + '" title="' + cbitxt + '" />'; 
        cbiinfo = new Array(cbiindex, cbitxt, cbi_image); 
        return cbiinfo; 
    }

  • #6
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi All,
    Here full code

    Code:
    function chandler ( inTemp, inHum ) {
      var temp = cTempToC( inTemp );
      var hum = inHum;
      
      cbi_index = (((110 - 1.373 * hum) - 0.54 * (10.20 - temp)) * (124 * Math.pow(10,-0.0142 * hum) ))/60;
      cbiindex =  cbi_index.toFixed(1); 
      
    
      
      if (cbi_index > 97.5) {
         cbitxt = langChandler[4];
        cbiimg= "/Fire32";
    
    } else if (cbi_index >= 90) {
         cbitxt = langChandler[3];
        cbiimg= "/Fire31";
    
    } else if (cbi_index >= 75) {
         cbitxt = langChandler[2];
        cbiimg= "/Fire25";
    
    } else if (cbi_index >= 50) {
        cbitxt = langChandler[1];
        cbiimg= "/Fire16";
    
    } else 
    
    if (cbi_index < 50) {
         cbitxt = langChandler[0];
        cbiimg= "/Fire0";
    
    }
      
      
      cbitxt = langChandlerMsg + ': ' + cbitxt;
      cbi_image = '<img src="' + imagedir2 + cbiimg + '.gif" alt="' + cbitxt + '" title="' + cbitxt + '" />';
      cbiinfo = new Array ( cbiindex, cbitxt, cbi_image );
      return cbiinfo;
    }
    Thanks Beau

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    PHP Code:
    function chandler inTempinHum ) {
      var 
    temp cTempToCinTemp );
      var 
    hum inHum;
      
      
    cbi_index = (((110 1.373 hum) - 0.54 * (10.20 temp)) * (124 Math.pow(10,-0.0142 hum) ))/60;
      
    cbiindex =  cbi_index.toFixed(1); 
        
      if (
    cbi_index 97.5) {
         
    cbitxt langChandler[4];
         
    cbiimg"/Fire32";

      } else if (
    cbi_index >= 90) {
         
    cbitxt langChandler[3];
        
    cbiimg"/Fire31";

      } else if (
    cbi_index >= 75) {
         
    cbitxt langChandler[2];
        
    cbiimg"/Fire25";

      } else if (
    cbi_index >= 50) {
        
    cbitxt langChandler[1];
        
    cbiimg"/Fire16";

      } else  {
         
    cbitxt langChandler[0];
         
    cbiimg"/Fire0";
      }
      
      
      
    cbitxt langChandlerMsg ': ' cbitxt;
      
    cbi_image '<img src="' imagedir2 cbiimg '.gif" alt="' cbitxt '" title="' cbitxt '" />';
      
    cbiinfo = new Array ( cbiindexcbitxtcbi_image );
      return 
    cbiinfo;


  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I mildly disagree.

    Code:
        cbiindex =  cbi_index.toFixed(1); 
        ...
        if (cbi_index >= 90)
    The first line there essentially rounds the number cbi_index to 1 decimal place.

    So if cbi_index was (example) 89.95, then the rounded value cbiindex will be 90.0

    That means cbi_index will *NOT* be >= 90 and so when the function returns you will have seemingly inconsistent results:
    Code:
        return [ "90.0", langChandler[2], "/Fire25" ];
    and the caller will wonder "Why Fire25 when clearly a value of 90.0 should give me Fire31???"

    I would suggest that a better way to do this would be
    Code:
    ...
      cbi_index = (((110 - 1.373 * hum) - 0.54 * (10.20 - temp)) * (124 * Math.pow(10,-0.0142 * hum) ))/60;
      cbiindex =  cbi_index.toFixed(1); 
      cbi_index = Number( cbiindex );
    So that the string value cbiindex and the numeric value cbi_index agree with each other.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Why not just have one variable instead of using 2 confusing var names, cbiindex and cbi_index

    Code:
    var cbi_index = (((110 - 1.373 * hum) - 0.54 * (10.20 - temp)) * (124 * Math.pow(10,-0.0142 * hum) ))/60;
    cbi_index =  Number(cbi_index.toFixed(1)); 
    ...
    cbiinfo = new Array ( cbi_index, cbitxt, cbi_image );

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,166
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Well, there *IS* a difference.

    Code:
    <script>
    var original = 83.078;
    var asText = original.toFixed(1);
    var asNumber = Number( asText );
    document.write( 1000000 * ( asNumber - 83 ) );
    </script>
    And that will display 99999.99999999431 whereas you would expect the answer to be 0.1 times 1000000 or 100000

    Good old floating point math just can't accurately represent decimal tenths, at all.

    So you may really want to ensure that you return a STRING that represents the number accurately to one decimal point rather than a number that doesn't.

    Likely to matter? No. But I was trying to be faithful to the original code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I moved this down
    Code:
     cbiindex =  cbi_index.toFixed(1);
    it was in the wrong place
    Beau

  • #12
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hi Glenn,
    why use
    Code:
    cbi_index =  Number(cbi_index.toFixed(1));
    why not just
    Code:
    cbi_index.toFixed(1);
    ?
    thanks Beau

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    toFixed returns a string. You need a number for comparison in the if-else block.

  • #14
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Quote Originally Posted by glenngv View Post
    toFixed returns a string. You need a number for comparison in the if-else block.
    It doesn't matter because when a number and a string are compared, the string is converted to a number. But one should be careful when both operands happen to be strings: then they are compared as strings, which is completely different to comparison as numbers.

  • #15
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    I'm aware of that. It's good programming practice to always compare with the same data type and not rely on internal casting.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •