SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Making shorthand from multiple IF / ELSE...

    I'm trying to make the following work in shorthand but I'm having some problems and I think it's something to do with the way I'm clarifying the condition...

    Original:
    Code:
    (class == \'test1\') ? file.href= dir+\'test1.txt\' : (class == \'test2\') ? test2.href= dir+\'test2.txt\' : file.href= dir+\'test3.txt\';
    My attempt at shorthand:
    Code:
    class == (\'test1\') ? file.href= dir+\'test1.txt\' : (\'test2\') ? file.href= dir+\'test2.txt\' : file.href= dir+\'test3.txt\';
    What am I doing wrong?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I see several inconsistancies between the "original" and "your attempt".

    Does the "original" work as it is?

    Does (\'string\') return either boolean true or false?

  3. #3
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Mittineague, and yes, it does work with the original. When I look at the "attempt" I keep thinking to myself that I'm doing something wrong with the comparison as most IF shorthand statements that I've found on the internet seem to be assignment clauses. I know it shouldn't matter, but I can't help to think that.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    If you try
    Code:
    var class = 'not_test_1';
    var orig = (class == \'test_1\');
    var attempt = class == (\'test_1\');
    alert(orig);
    alert(attempt);
    Do they both alert FALSE?

  5. #5
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note that class is a reserved word in ECMAScript and that it would be a really, really bad idea to use it as a variable name.
    Birnam wood is come to Dunsinane

  6. #6
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the heads-up on that Tommy. I didn't realize that. But yes, Mittineague, they both throw up "false" when the page loads.

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Another area where there's differences is
    Code:
    : (class == \'test2\') ? test2.href= dir+\'test2.txt\' :
    : (\'test2\') ? file.href= dir+\'test2.txt\' :
    Does
    Code:
    var class = 'not_test2';
    var orig = (class == \'test2\');
    var attempt = (\'test2\');
    alert(orig);
    alert(attempt);
    Do both alerts still show boolean TRUE or FALSE ??

    And is there a difference between using test2.href and file.href ??

  8. #8
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mittineague - First one shows "false" and the other shows "test2."

    And for the difference, yes, there will be. In the example I provide, there isn't, but in the actual website, the 2 different files each have their own different contents.

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,228
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I'm guessing that the problem is the "test2". AFAIK ternary conditionals need to return either boolean TRUE or FALSE, not a string. So if you change it to something like
    Code:
    : class == (\'test2\') ? file.href= dir+\'test2.txt\' :
    that should fix things up, other than not using a reserved word for the variable name issue.

    And I'm not sure that those escapes and parentheses are needed, but I guess that depends on what context you're using this in.


Bookmarks

Posting Permissions

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