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.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts

    how to shrink code ? sample:

    Realy annoyed by this code but cant do it any other way.
    Was trying using ?:; but no success.
    So: any suggestion how to do the same with much lesss code ?
    Thanks.

    Code:
          result = checkNick(nick);
          if (result !='')
          {
            alert(result);
            return;    
          }    
          
          result = checkMail(email);
          if (result !='')
          {
            alert(result);
            return;    
          }    
          
          result = checkPwd(pwd1,pwd2);
          if (result !='')
          {
            alert(result);
            return;    
          }   

  • #2
    New Coder
    Join Date
    Dec 2007
    Posts
    14
    Thanks
    0
    Thanked 5 Times in 5 Posts
    try this


    Code:
    result = checkNick(nick) || checkMail(email) || checkPwd(pwd1,pwd2);
    if (result)
        alert(result);

  • Users who have thanked arilia for this post:

    BubikolRamios (01-05-2008)

  • #3
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    nope, mybe this would work if return type would be boolean, but it is string.

  • #4
    New Coder
    Join Date
    Dec 2007
    Posts
    14
    Thanks
    0
    Thanked 5 Times in 5 Posts
    well, it works on strings too. I tested it on FF and on IE6.

    But you can try this too

    Code:
    result = checkNick(nick)? checkNick(nick) :(checkMail(email)? checkMail(email): checkPwd(pwd1,pwd2));
    Anyway what kind of error you get?
    Last edited by arilia; 01-02-2008 at 11:15 AM.

  • #5
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by arilia View Post
    Code:
    result = checkNick(nick) || checkMail(email) || checkPwd(pwd1,pwd2);
    if (result)
        alert(result);
    Works for me.
    Forget style. Code to semantics. Seperate style from structure, and structure from behaviour.
    I code to specs, and test only in Firefox (unless stated otherwise).

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    var a = [checkNick(nick), checkMail(email), checkPwd(pwd1, pwd2)];
    for(var i = 0, n = a.length; i < n; ++i)
      if(a[i].length == 0) {
        alert(a[i]);
        return;
      }
    Trinithis

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by BubikolRamios View Post
    nope, mybe this would work if return type would be boolean, but it is string.
    strings are essentially booleans in javascript:

    ""==false
    "false" == true
    "0"==true
    "anything but blank" == true
    Last edited by rnd me; 01-03-2008 at 09:57 PM. Reason: spell
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #8
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,994
    Thanks
    120
    Thanked 76 Times in 76 Posts
    thanks to all.


  •  

    Posting Permissions

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