If AND with 3 conditions apps script - google-apps-script

Im trying to make somthing like this:
IF( A>B && A>C && A >= D){
than do this;
}
¿Can an IF have 3 conditions?

There can be any number of conditions in an if() statement.
Apps Script is JavaScript and therefore case-sensitive, and keywords such as if are spelled in lower case. It is a good practice to use lower case with variable names as well, like this:
if (a > b && a > c && a >= d) {
// ...do something
}
See if...else.

Related

Is there an alternative way to filter xml elements without the E4X syntax?

I am trying to compile some old actionscript code (part of flash app) to JS using Jangaroo. Jangaroo does not support the E4X syntax and it fails at things like the double-dot operator .. or the brackets filters a.(CONDITION). So I need to rewrite those portions of code using plain ActionScript.
For the double-dot operator, I used the instead the method descendants() but I could not find alternative way to write the brackets filter.
Here is the original code I had:
B = xml..destination.(#id == someId)
I wrote it now:
B = xml.descendants("destination").(#id == someId)
But I still want to remove .(#id == someId).
I am thinking of something like:
if (xml.descendants("destination").attribute("id") == someId)
{
B = xml.descendants("destination")
}
Is this possible?
So here is how I proceeded. I have not tested its functionality, but the compiler passed it.
var destinations:XMLList = null;
for each (var elm in xml.descendants("destination") )
{
if ( elm.attribute("id") == someId )
{
destinations += elm;
}
}

in mysql we have SOUNDEX() or we can use SOUNDS LIKE for related / wrong spells or words matching so is their any thing like in BIGQUERY

in mysql we have
SOUNDEX()
and
SOUNDS LIKE
for related / wrong spellings or words matching in query
Is there anything like that or similar in BigQuery? Can BigQuery be used for such queries?
Using BigQuery User-Defined Functions (UDF) in JavaScript you can simply implement SOUNDEX function yourself.
Here is SQL statement for BigQuery that calculate SOUNDEX:
#standardSQL
CREATE TEMP FUNCTION SOUNDEX(name STRING)
RETURNS STRING
LANGUAGE js AS """
if (name == null) return null;
let s = [];
let si = 1;
let c;
// ABCDEFGHIJKLMNOPQRSTUVWXYZ
let mappings = "01230120022455012623010202";
s[0] = name[0].toUpperCase();
for(let i = 1, l = name.length; i < l; i++) {
c = (name[i].toUpperCase()).charCodeAt(0) - 65;
if(c < 0 || c > 25) { continue; }
if(mappings[c] == '0') { continue; }
if(mappings[c] != s[si-1]) {
s[si] = mappings[c];
si++;
}
if(si > 3) { break; }
}
if(si <= 3) {
while(si <= 3) {
s[si] = '0';
si++;
}
}
return s.join("");
""";
SELECT SOUNDEX("John Doe"), SOUNDEX("Jon Do")
Credit: I took the original JavaScript code from Chris Webb blog post here and slightly change the coding syntax.
I've checked in the "Google BigQuery Analytics" book, and no, BigQuery does not have anything like "SOUNDEX()" (at least at the time of publishing).
You might want to check cloudSQL, which is a mysql server hosted on the cloud. I know there are some functions from mysql you can't use in the cloudSQL, but it would be worth looking into.
I realize this question was asked years ago, but for those who wander here as I did looking for answers, here you go.
BigQuery does now have SOUNDEX()
Here is the documentation

Actionscript 3.0....x>y>z not working

probably a pretty simple question here, but I find it weird. Luckily I found a way around it, but the fact that what I did works and what I have in the title doesn't work is confusing the hell out of me!
I just have a simple if statement...then execute a function. This code works:
if (200 > (x-target.x) && (x-target.x) > 0)
fireWeapon();
yet this code doesn't!
if (200 > (x-target.x) > 0)
fireWeapon();
AS3 does not give me an error either....It just simply does an if statement for the condition
if (200 > (x-target.x))
and seems to ignore the statement where it must be greater than 0. I would like to use the shorter, more mathematically nice looking method in the future, so let me know if there is a way around doing the && sign! Thanks.
if (200 > (x-target.x) > 0) code is working. but what you think is different. computer in order to interpret one sentence. Is evaluated as follows.
1) 200 > ( x-target.x ) If 200 is larger than (x-target.x) return true, not false.
2) true(1) or false(0) > 0 If left-statement is true return true,(because 1 is larger than 0) not false.
As a result, If 200 is larger than (x-target.x) always return true, not false. In general, the syntax used in the computer language is not the same as mathematic syntax.
And you want x> y> z must change to x>y && y>z && x>z.

is number between 2 values

What's the simplest way in actionscript to find if number is between -20 and +20, and return a true/false? I can see there's a number validator but I see it involves firing and catching events, which I think maybe overkill for the simple test I'm trying to do here.
Simplest way would be comparing the number with both values and logical combine the results:
return num > -20 && num < 20;
You may use >= or <= to include the values if needed.
You can make that into a nice function:
function isBetween(num:Number, lowerBound:Number, upperBound:Number):Boolean {
return num > lowerBound && num < upperBound;
}
Just write a function, conceptually like this:
protected function validatateNumbers(value:Number):Boolean{
if((value > -20) && (value <20)){
return true;
}
return false;
}
Then call the function whenever you want to validate your input.

Most readable way to write simple conditional check

What would be the most readable/best way to write a multiple conditional check such as shown below?
Two possibilities that I could think of (this is Java but the language really doesn't matter here):
Option 1:
boolean c1 = passwordField.getPassword().length > 0;
boolean c2 = !stationIDTextField.getText().trim().isEmpty();
boolean c3 = !userNameTextField.getText().trim().isEmpty();
if (c1 && c2 && c3) {
okButton.setEnabled(true);
}
Option 2:
if (passwordField.getPassword().length > 0 &&
!stationIDTextField.getText().trim().isEmpty() &&
!userNameTextField.getText().trim().isEmpty() {
okButton.setEnabled(true);
}
What I don't like about option 2 is that the line wraps and then indentation becomes a pain. What I don't like about option 1 is that it creates variables for nothing and requires looking at two places.
So what do you think? Any other options?
if (HasPassword() && HasStation() && HasUserName())
okButton.setEnabled(true);
bool HasPassword() {
return passwordField.getPassword().length > 0;
}
etc.
Note that option 1 does not allow for short circuiting behavior. That is, you calculate the value of all of the conditionals before evaluating the result of the first.
I would modify option 1 so that you're using variable names that actually have a meaning. That is, change the name of "c2" to be something like "stationIDIsEmpty" (and move the NOT into the conditional). That way the conditional is readable without having to glance back and forth for every variable.
So my code would probably look like:
boolean enteredPassword = passwordField.getPassword().length > 0;
boolean stationIDIsEmpty = stationIDTextField.getText().trim().isEmpty();
boolean userNameIsEmpty = userNameTextField.getText().trim().isEmpty();
if (enteredPassword && !stationIDIsEmpty && !userNameIsEmpty) {
okButton.setEnabled(true);
}
I voted for Chris Brandsma's answer.
But just wanted to mention the main issue I have with Option 1 is you are losing the benefit of &&. With option one, although I think it's more readable, you are processing comparisons when they may not be required.
Personally, I like the second way, because I find that using that way can make the predication of the conditionals clear. That is, with that method done properly, you can make the conditional comprehensible by "verablizing" it (whether or not you actually speak it is irrelevant).
That is, with your second option, it becomes clear that your conditional translates roughly as this: "If the password length is greater than zero, AND the stationIDTextField (trimmed) is NOT empty, AND the usernameTextField (trimmed) is NOT empty, then..."
I prefer the following:
if (passwordField.getPassword().length > 0
&& ! stationIDTextField.getText().trim().isEmpty()
&& ! userNameTextField.getText().trim().isEmpty())
{
okButton.setEnabled(true);
}
With this coding style I accomplish two things:
I can easily see that each extra line of the if is part of the condition because of the && (or ||) at the beggining.
I can easily see where the if statement ends because of the { at the next line.
Option1 is prime for applying the refactoring 'Replace temp with Query'. The reason being that someone can stuff in code between the variable is initialized and the check and change the behavior of the code. Or the check might be made with stale values.. an update has been made to the textfields between initialization and checking.
So my attempt at this would be
if (GetPasswordLength() > 0
&& FieldHelper.IsNotEmpty(stationIDTextField)
&& FieldHelper.IsNotEmpty(userNameTextField)
{
okButton.setEnabled(true);
}
FieldHelper is a class with public static methods (also called a Utility class / Static class in C#)