Comparing Values
-
I'm trying to compare values again each other to do some function, I got this so far, can Anyone help? I know I probably can put all this into and array but don't know how to.
inline function onbassfreqBtnControl(component, value) { if(value) bassfreqPnl.set("visible", true); else bassfreqPnl.set("visible", false); if(value == 1) AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false); }; Content.getComponent("bassfreqBtn").setControlCallback(onbassfreqBtnControl); inline function onmidfreqBtnControl(component, value) { if(value) midfreqPnl.set("visible", true); else midfreqPnl.set("visible", false); if(value == 1) AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false); // Comparing Elements (Buttons) to close the AnalyserPnl and shuts off the Btn if(bassfreqBtn != midfreqBtn != treblefreqBtn != presencefreqBtn) // not equal to //if(bassfreqBtn == midfreqBtn == treblefreqBtn == presencefreqBtn) // equal to AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false); }; Content.getComponent("midfreqBtn").setControlCallback(onmidfreqBtnControl); inline function ontreblefreqBtnControl(component, value) { if(value) treblefreqPnl.set("visible", true); else treblefreqPnl.set("visible", false); if(value == 1) AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false); }; Content.getComponent("treblefreqBtn").setControlCallback(ontreblefreqBtnControl); inline function onpresencefreqBtnControl(component, value) { if(value) presencefreqPnl.set("visible", true); else presencefreqPnl.set("visible", false); }; Content.getComponent("presencefreqBtn").setControlCallback(onpresencefreqBtnControl);
-
@Jay said in Comparing Values:
Let's start with this
if(value) bassfreqPnl.set("visible", true); else bassfreqPnl.set("visible", false); if(value == 1) AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false);
You have two if statements that are both checking the same value, so why not just use one?
if (value) { bassfreqPnl.set("visible", true); AnalyserPnl.set("visible", true); } else { bassfreqPnl.set("visible", false); AnalyserPnl.set("visible", false); }
But we can make this even better. When
value == 1
we are setting the two panels totrue
and since1
andtrue
are the same thing we can get rid of the if statement entirely and set the panel's visibility tovalue
.bassfreqPnl.set("visible", value); AnalyserPnl.set("visible", value);
Now let's look at this
if(value) midfreqPnl.set("visible", true); else midfreqPnl.set("visible", false); if(value == 1) AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false); // Comparing Elements (Buttons) to close the AnalyserPnl and shuts off the Btn if(bassfreqBtn != midfreqBtn != treblefreqBtn != presencefreqBtn) // not equal to //if(bassfreqBtn == midfreqBtn == treblefreqBtn == presencefreqBtn) // equal to AnalyserPnl.set("visible", true); else AnalyserPnl.set("visible", false);
So again those first two if statements are unnecessary. We can rewrite them the same way as the previous part.
midfreqPnl.set("visible", value); AnalyserPnl.set("visible", value);
Now the next part
if(bassfreqBtn != midfreqBtn != treblefreqBtn != presencefreqBtn)
When you want to check the value of a button you need to use the
.getValue()
function. And when you want to do more than one comparison in a statement you need to use logical operators such as && (which means and), || (which means or). You can look up javascript operators on the internet to find the complete list and their use.There are several ways to rewrite the if statement above but I'll just show you the way I'd do it.
if (!bassfreqBtn.getValue() && !midfreqBtn.getValue() && !treblefreqBtn.getValue() && !presencefreqBtn.getValue()) //Not equal
However I wouldn't do this as an if statement, because all you are doing in the if statement is setting the visible property of a panel which is a
true/false
,0/1
value as we had before. And we can get the value directly from the line above and store it in a local variable. I'm not sure if you want it to be be visible when equal or visible when not equal but if you want the opposite behaviour to what I've put below just remove all the!
and it will flip the value.local v = !bassfreqBtn.getValue() && !midfreqBtn.getValue() && !treblefreqBtn.getValue() && !presencefreqBtn.getValue(); //Not equal AnalyserPnl.set("visible", v);
We could drop the local variable and just put the comparison directly in the
.set()
function as the second parameter but I think it reads better to have it stored in a variable and might be useful if you decide to add more code that depends on the same value.