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 12 of 12
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    36
    Thanks
    9
    Thanked 1 Time in 1 Post

    Is there a way to do this more efficiently?

    So, say I have


    else if (variable1 == 3) {
    document.getElementById("variable2").value = 1;

    else if (variable1 == 4) {
    document.getElementById("variable2").value = 2;


    }
    And I would like to change it so

    else if (variable2 == 1) {
    document.getElementById("variable1").value = 3;

    else if (variable2 == 2) {
    document.getElementById("variable1").value = 4;


    }
    How would I do that?
    I have dreamweaver(so I can replace some string with another easily). And should i use switch..break statement instead so I can easily change variable1 with variable2?
    btw, I have lots of those statements...

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Ummm...type the code in?

    What do you mean by "change it"?

    You mean "at a certain point in the page, I want to switch the operation to go the other way"???

    Or what?

    Code:
    I have dreamweaver(so I can replace some string with another easily).
    Ah, well. I do feel sorry for you, then. NightmareMaker (its real name) has destroyed many an otherwise good page by creating the worst possible JavaScript (and PHP and ASP) code it can.
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You appear to be assigning the same name to HTML elements and Javascript variables. This is strongly contra-indicated!

    In this situation you should use a switch statement to avoid multiple if....else.

    I don't really understand your question. Just make the changes desired! But I have to say that Dreamweaver is not highly thought of around here.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    A better all around way:
    Code:
    var valueChanges = {
       "v3" : 1,
       "v4" : 2
    };
    
    document.getElementById("variable2").value = valueChanges( "v" + variable2 );
    And then you could just change valueChanges to
    Code:
    valueChanges = {
       "v1" : 3,
       "v2" : 4 
    };
    But that assume you mean you want this change to be dynamic, during the processing of the page.
    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.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Whoops! I didn't notice that the variable names were reversed in the second set!

    Shame on me. Blame my old eyes.

    So basically you mean you want a two-way lookup table?

    1 ==>> 3
    but then
    3 ==>> 1
    ?
    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.

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    36
    Thanks
    9
    Thanked 1 Time in 1 Post
    ^Yes, a two-way look up table. That's the word I was looking for.

  • #7
    New Coder
    Join Date
    Jun 2012
    Posts
    36
    Thanks
    9
    Thanked 1 Time in 1 Post
    Ok, Im using switch statements now.

    So I now have

    Code:
    	switch (variable1){
    		  case 1:
    			  variable2 = 3;
    			  break;
    		  case 4:
    			  variable2 = 2;
    			  break;
    
    ...
    		}
    	document.getElementById("displayvariable").value = variable2;
    }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    Code:
    var lookup = {
        "v1" : 3,
        "v2" : 4
    };
    
    var reverseLookup = { };
    
    for ( var v in lookup )
    {
        reverseLookup["v" + lookup[v]] = Number(v.substring(1));
    }
    and then you can do
    Code:
    document.getElementById("variable2").value = lookup["v" + variable1];
    and
    document.getElementById("variable1").value = reverseLookup["v" + variable2];
    You could, of course, code reverseLookup by hand, but you can also just create it by iterating through lookup, as above.
    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.

  • Users who have thanked Old Pedant for this post:

    cord (07-20-2012)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    By the way, the reason for the "v" prefixes is simply that you can't have an object member with a name that starts with a digit. You could use underline or any letter or whatever instead of "v", of course.
    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.

  • #10
    New Coder
    Join Date
    Jun 2012
    Posts
    36
    Thanks
    9
    Thanked 1 Time in 1 Post
    Thanks, I'm having a little trouble implementing it though. Ill try a few things, but I may need your help later!

  • #11
    New Coder
    Join Date
    Jun 2012
    Posts
    36
    Thanks
    9
    Thanked 1 Time in 1 Post
    Ok, I'm confused, but I still need to switch statements right?


    say for example. I input a 1 into input1. I want input2 to be a 3. And vice versa.


    Code:
    <!DOCTYPE html>
    <html lang="en">
    
      <head>
        <meta charset="utf-8" />
        <meta name="description" content="None" />
        <title>None</title>
        <script type="text/javascript" src="test.js"></script>
      </head>
    
      <body>
    
      Input1<br />
    <input type="text" id="variable1" size="20"/><br /><br />
      Input2<br />
    <input type="text" id="variable2" size="20"/><br /><br />
    
    <input type="Button"  id="button" value="Calculate" />
    
      </body>
    </html>

    Code:
    window.onload = function (){
    
    document.getElementById("button").onclick = ButtonClick;
    var variable1 = parseInt(document.getElementById("variable1").value,10);
    var variable2 = parseInt(document.getElementById("variable2").value,10);
    
    
    var lookup = {
        "v1" : 3,
        "v2" : 4
    };
    
    var reverseLookup = { };
    
    for ( var v in lookup )
    {
        reverseLookup["v" + lookup[v]] = Number(v.substring(1));
    	
    	
    function ButtonClick(){
    		switch (variable1){
    		  case 1:
    			  variable2 = 3;
    			  break;
    		  case 4:
    			  variable2 = 2;
    			  break;
    		}
    document.getElementById("variable2").value = lookup["v" + variable1];
    document.getElementById("variable1").value = reverseLookup["v" + variable2];		
    }
    }
    }

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,225
    Thanks
    80
    Thanked 4,456 Times in 4,421 Posts
    NO! This is a REPLACEMENT for the switch. And more efficient. And less code.

    Code:
    window.onload = function (){
        var lookup = {
            "v1" : 3,
            "v2" : 4
        };
        var reverseLookup = { };
        for ( var v in lookup )
        {
            reverseLookup["v" + lookup[v]] = Number(v.substring(1));
        }
    
        document.getElementById("button").onclick = function() {
            var v1 = parseInt(document.getElementById("variable1").value,10);
            var v2 = parseInt(document.getElementById("variable2").value,10);
            document.getElementById("variable2").value = lookup["v" + v1];
            document.getElementById("variable1").value = reverseLookup["v" + v2];		
        }
    }
    Also, as Philip said, try to avoid using variables with the same names as id's of elements.

    Also, if you would put this code at the *BOTTOM* of your page, you wouldn't need the window.onload, which can cause problems, if other scripts on the page are also trying to use window.onload.

    That is, do this:
    Code:
    <html>
    ...
    <body>
    ... content ...
    
    <script type="text/javascript">
        var lookup = {
            "v1" : 3,
            "v2" : 4
        };
        var reverseLookup = { };
        for ( var v in lookup )
        {
            reverseLookup["v" + lookup[v]] = Number(v.substring(1));
        }
    
        document.getElementById("button").onclick = function() {
            var v1 = parseInt(document.getElementById("variable1").value,10);
            var v2 = parseInt(document.getElementById("variable2").value,10);
            document.getElementById("variable2").value = lookup["v" + v1];
            document.getElementById("variable1").value = reverseLookup["v" + v2];		
        }
    </script>
    </body>
    </html>
    Presto.
    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.

  • Users who have thanked Old Pedant for this post:

    cord (07-22-2012)


  •  

    Posting Permissions

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