• I be emm386. (unregistered) in reply to Someone You Know

    Once upon a time in a place far far away Programmers were paid by the line. As a result LOCs made it's way into the lexicon of history and software found "Easter Eggs" [Gasp!] and the like.

    Could this be a remnant of times gone buy (sic)?

  • (cs) in reply to attemp1

    If you use a binary tree...

                 1-25                           25-50
    
            1-12     13-25              25-37   38-50
    
       1-6 7-12    13-19 20-25    25-31 32-37    38-44 45-50
    

    1-3 4-6 7-10 11-12 etc etc etc

    You'd make at most 5 compares?

  • (cs) in reply to playerone

    And add a hashtable for the labels...

  • cc (unregistered)
    var n = 0; var f =0;

    This right here. This was as far as I had to read to know this code was going to be bad. When I see sloppiness like this, I know right away that the "programmer" is just clumsily ham-fisting everything in while alt-tabbing back and forth between Google cached expert-exchange.com threads.

    Captcha: laoreet. Clearly a reference to Lao-Tse's Tao of Hax0ring.

  • WarrenFaith (unregistered) in reply to attemp1

    what about this?

    switch (elementItem) {
      case "header1:SearchBox" :
        __doPostBack('header1:goSearch','');
        break;
    
      default:
        window.event.returnValue=false;
        window.event.cancel = true;
        document.forms[0].elements[n+1].focus();
        break;
      }
    }
  • Micah (unregistered)

    This is shorter.

    if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else if (elementItem) { textName = elementItem.substr(0,3); textNum = elementItem.substr(4); if (textName == 'Text' and textNum >= 1 and textNum <= 50) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

  • coyo (unregistered) in reply to attemp1

    <quote>I just can't see anything else that would make it smaller, expandable and easier to manage...</quote>

    How about a regular expressions? Javascript has them

    elementItem.matches(/^Text([1-5][0-9]|[0-9]$/);

  • (cs) in reply to coyo
    coyo:
    I just can't see anything else that would make it smaller, expandable and easier to manage...

    How about a regular expressions? Javascript has them

    elementItem.matches(/^Text([1-5][0-9]|[0-9]$/);

    irony, adj. consisting of iron, resembling iron

  • Mark (unregistered)

    How about:

    switch (elementItem) { case "header1:SearchBox" : __doPostBack('header1:goSearch',''); break; default: window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

  • Object (unregistered) in reply to attemp1

    close... but I'd just put the case stuff in a separate function. Example

    form = document.forms[0]; function doThatThing(form ) { window.event.returnValue=false; window.event.cancel = true; form.elements[n+1].focus(); break;

    }

    Everything between the lowest and highest variable name should not even be cases since nothing is really changing here.

  • Brian (unregistered) in reply to attemp1

    This code just screams to be an ifelse. Also I hate seeing switches with no default: case. You could optionally use a regex in the else statement to make sure it matches the "Text"n+ format.

    if(elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; }

  • Dmitry Olyenyov (unregistered) in reply to attemp1

    witch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } default: { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

  • D (unregistered) in reply to attemp1

    Not even breaking up the string, parsing as an Int and just checking if it's 0 < element < 51 ?

  • prickett (unregistered) in reply to attemp1

    A switch() statement shouldn't be used at all.

    if ( elementItem == header1:SearchBox ) {

    } else if ( elementItem.substring(0,3) == 'Text' ) {

    }

  • Rafael Coutinho (unregistered)
    function submitTrap3()
    {
        var n = 0;
        var f =0;
        var elementItem = "";
        if (window.event.keyCode == 13)
        {
         for (n=0;n<document.forms[0].elements.length;n++)
         {
            elementItem = document.forms[0].elements[n].name;
            if (
                (document.forms[0].elements[n].value != "") 
                 &&
               (elementItem == trim(document.forms[0].txtControlName.value)))
                {
      	    if(elementItem == "header1:SearchBox"){ 						__doPostBack('header1:goSearch','');
                break;
    	    }else{
                  if(elementItem.substring(0,4) == "Text"){
    		var value = elementItem.substring(4);
    	        if(value>=1 && value<=50){
    						       
                         window.event.returnValue=false; 
                         window.event.cancel = true;
     	             document.forms[0].elements[n+1].focus();
                       break;
                     }
                  }
               }
              }   
             }
        }
    }
    
  • nodeworx (unregistered) in reply to attemp1

    one if in a loop, break out of the loop if the condition is met... -ouch

  • Steve (unregistered) in reply to attemp1

    if(elementItem=='header:SearchBox') _doPostBack('header1:goSearch',''); else { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

  • David (unregistered) in reply to attemp1

    Even the replies are stupid... hack it like this:

    if (elementItem.substr(0, 4) == "Text") { var num = parseInt(elementItem.substr(4)); if (num > 0 && num < 51) { window.event.returnValue = false; window.event.cancel = true; } document.forms[0].elements[n+1].focus(); } else if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); }

  • JakeBurkey (unregistered) in reply to JoelKatz

    ROFL!

  • (cs) in reply to attemp1
    attemp1:
    how about this?

    switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": case "Text2": case "Text3": case "Text4": case "Text5": case "Text6": case "Text7": case "Text8": case "Text9": case "Text10": case "Text11": case "Text12": case "Text13": case "Text14": case "Text15": case "Text16": case "Text17": case "Text18": case "Text19": case "Text20":
    case "Text21":
    case "Text22":
    case "Text23":
    case "Text24":
    case "Text25": case "Text26": case "Text27": case "Text28": case "Text29": case "Text30": case "Text31": case "Text32": case "Text33": case "Text34": case "Text35": case "Text36": case "Text37": case "Text38": case "Text39": case "Text40": case "Text41": case "Text42": case "Text43": case "Text44": case "Text45": case "Text46": case "Text47": case "Text48": case "Text49": case "Text50": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

    I just can't see anything else that would make it smaller, expandable and easier to manage...

    "Case Else" ?

  • Widget (unregistered)

    for (int i=1; i++; i < 51) { if (elementItem == "Text"+i) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); } }

    Or am I just being a noob?

  • joeldg (unregistered) in reply to attemp1

    well you could loop though 1-50 and concat them into variables i.e. ${'Text'+str(loopVar)}

  • Fedde (unregistered)

    Reducing the line count by two or three might actually be harder than reducing it by about 344.

  • Anonymous (unregistered)

    Well, it's obvious that you ought to dynamically figure out what Text number it is...

    if (elementName.match("Test") && parseInt(elementName.split("Test")[1])>=1 && parseInt(elementName.split("Test")[1])<=50) {
    	//do stuff...
    }
    
  • Arg.123 (unregistered)

    //c# code sample

    if(elementItem=="header1:SearchBox")__doPostBack('header1:goSearch',''); else for(int c=1;c<51;c++)
    if(elementItem=="Text"+c) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

  • Anonymous (unregistered) in reply to attemp1

    default:

  • Anonymous (unregistered) in reply to attemp1

    if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); break; } else if (elementItem.match(/^Text\d\d) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; }

    ...Please tell me you were joking.

  • CW (unregistered)

    Well, this is a full 4% (609 bytes compared to 15210) if they used spaces, or 6.65% if they used tabs, of the code and does exactly what it does. Probably faster.

    function submitTrap3() {
    	var e = window.event;
    	if (e.keyCode == 13) {
    		var form = document.forms[0], elements = form.elements;
    		var elementItem = "", val = trim(form.txtControlName.value);
    		var ele;
    		for (var n = 0, f = elements.length; n < f && (ele = elements[n]).value; n++) {
    			elementItem = ele.name;
    			if (elementItem == val) {
    				if (elementItem == "header1:SearchBox") {
    					__doPostBack('header1:goSearch','');
    				} else if (/Text(?:[1-9]|[1-4]\d|50)/.test(elementItem)) {
    					e.returnValue = false;
    					e.cancel = true;
    					elements[n+1].focus();
    				}
    			}
    		}
    	}
    }

    and if

    form.txtControlName.value
    is the name of the selected/submitted field like i suspect it is, this is even better.

    function submitTrap3() {
    	var e = window.event;
    	if (e.keyCode == 13) {
    		var form = document.forms[0], elements = form.elements;
    		var val = trim(form.txtControlName.value);
    		var ele = form.getElementsByName(val)[0];
    		if (ele && ele.value) {
    			if (val == "header1:SearchBox") {
    				__doPostBack('header1:goSearch','');
    			} else if (/Text(?:[1-9]|[1-4]\d|50)/.test(val)) {
    				e.returnValue = false;
    				e.cancel = true;
    				elements[elements.indexOf(ele) + 1].focus();
    			}
    		}
    	}
    }

Leave a comment on “If I've Said It Once, I've Said It Fifty Times”

Log In or post as a guest

Replying to comment #:

« Return to Article