The if-clause is equivalent to the Decision
element that we used in the last chapter. It checks whether a condition has been met.
The JavaScript code for an if
statement isn't that difficult. It is made up of a condition and the if-clause itself. The form looks like this:
Statement |
Example |
---|---|
if (condition) { code block } else if (condition) { code block } else { code block } | if (varString == "test") { varResult=true; } else { varResult=false; } |
In the preceding example, the varString
string is tested if it contains the word "test"
. If it does, the Boolean varResult
is set to true
. If it is not (the else
part), it is set to false
. The else if
block can be added multiple times.
A condition is made up of a statement that is either true or false. This statement is built by using operators. The following are the operators that JavaScript knows:
and |
or |
Not |
Equal |
Not equal |
Smaller |
Bigger |
---|---|---|---|---|---|---|
|
|
|
|
|
|
|
As an example, if you want to know whether the number that is stored in the a
variable is bigger than 5, the conditional statement is (a > 5)
.If you want to know whether the string stored in the text
variable equals "Hello"
, the conditional statement will be (text == "Hello")
.
You can glue conditions together with the &&
(and) and ||
(or) operators as well as (), the normal brackets. For example, if a is bigger than 5 and the text is "Hello", the conditional statement will be ((a>5) && (text == "Hello"))
.
In the example part of this section, we will give all of this a try.
Now, let's have a look at an example. We will use our JavaScript example workflow again:
Name |
Where |
Type |
---|---|---|
myFirstValue |
INPUT |
Number |
mySecondValue |
INPUT |
String |
myOutput |
OUTPUT |
String |
if (myFirstValue>5) { myOutput="bigger than 5"; } else { myOutput="smaller or equal than 5"; }
"Hello"
(the correct answer is given at the end of the chapter)."Hello"
at the same time (the correct answer is provided at the end of the chapter).We will integrate a more complex if-Clause into our deploy program in the section where we learn about actions.
I would like to showcase some typical string problems that you will encounter when working with VMs. The String type comes in JavaScript with some methods (functions) that you can use. You can find all the JavaScript String methods at http://www.w3schools.com/jsref/jsref_obj_string.asp.
This is a typical situation that you will come across. You have a parameter that contains a string, and you would like to know whether this string contains another string.
For example, you have the myVM parameter that contains the "testVM.mylab.local"
string, and you want to know whether this VM is in the mylab domain. So, if the "myLab"
string is a part of it, you can use the indexOf()
method of the String type. The index method returns the position of the first occurrence. If the string is not a part of the other string, the return value will be -1. Here's an example:
Var myVM = "testVM.mylab.local"; If ( myVM.indexOf("myLab") != -1) { //is part of mylab } else { //is NOT part of mylab }
Another common problem is that a user might enter MyLab
, myLab
, or MYLAB
instead of the myLab
that you are expecting and checking for. Here is a simple way to solve this one. You can just keep everything in uppercase and then check. So, don't use the following code:
var myEntry = "MyLab"; var myTest = "myLab": if ( myEntry == myTest ) { // both are the same }
Instead of the preceding code, use the .toUpperCase()
method of the String type, as follows:
If ( myEntry.toUpperCase() == myTest.toUpperCase() ) { // both are the same }
The same method applies to hostnames or VM names. VM names or hostnames are mostly in lowercase. Use the .toLowerCase()
method to do this.
Consider an instance where users enter "myLab "
or " myLab"
by mistake (an extra space at the beginning or the end), and you can imagine how this can wreak havoc with an if-clause. Use the .trim()
method to solve this, as follows:
var dirtyString = " Hello World! "; var cleanString = dirtyString.trim();