...

View Full Version : Is there a way to do this more efficiently?



cord
07-20-2012, 08:31 PM
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...

Old Pedant
07-20-2012, 08:41 PM
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?


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.

Philip M
07-20-2012, 08:43 PM
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.

Old Pedant
07-20-2012, 08:45 PM
A better all around way:


var valueChanges = {
"v3" : 1,
"v4" : 2
};

document.getElementById("variable2").value = valueChanges( "v" + variable2 );

And then you could just change valueChanges to


valueChanges = {
"v1" : 3,
"v2" : 4
};


But that assume you mean you want this change to be dynamic, during the processing of the page.

Old Pedant
07-20-2012, 08:47 PM
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
?

cord
07-20-2012, 08:58 PM
^Yes, a two-way look up table. That's the word I was looking for.

cord
07-20-2012, 09:27 PM
Ok, Im using switch statements now.

So I now have



switch (variable1){
case 1:
variable2 = 3;
break;
case 4:
variable2 = 2;
break;

...
}
document.getElementById("displayvariable").value = variable2;
}

Old Pedant
07-20-2012, 09:36 PM
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


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.

Old Pedant
07-20-2012, 09:40 PM
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.

cord
07-20-2012, 09:58 PM
Thanks, I'm having a little trouble implementing it though. Ill try a few things, but I may need your help later!

cord
07-20-2012, 10:36 PM
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.



<!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>



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];
}
}
}

Old Pedant
07-20-2012, 11:39 PM
NO! This is a REPLACEMENT for the switch. And more efficient. And less 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:


<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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum