JavaScript in Plain Language (2015)
PART I: AT THE GATES OF ENLIGHTENMENT
1.8 Lab work 3
Let’s practice a bit to cement the concepts covered in this chapter.
Please fire up your Console with CTRL SHIFT j
or CMD SHIFT j for Mac
or ALT-CMD-j.
Part one: working with +, += , ++ and --
1- Add 3 and 4
2- Declare a variable x with the value of 3 and
a variable y with the value of 5.
Then create variable z and assign to it the sum of x and y.
Call variable z, it should contain the value 8.
3- x is 3. Increment x to 4 using the unary operator ++. Then call x to see the result.
4- Decrement x by 1, using the unary operator --. Then call x to see the result.
5- Concatenate "Hello" to "World!". Make sure to include a blank space between the two words.
6- Declare a variable named myName and assign it only the first character of your name.
7- Now add the second character of your name to myName using the += operator.
8- Do the same for all the other characters of your name. Call variable myName to see the result.
(see the results on the next page)
Part one results
1- Add 3 and 4:
3 + 4;
2- Declare variable x with the value of 3, and variable y with the value of 5. Then create variable z and assign to it the sum of x and y. Call variable z, it should contain the value 8.
var x = 3;
var y = 5;
var z = x + y;
z; <--- should be 8
3- x is 3. Increment x to 4, using the unary operator ++. Then call x to see the result.
x++; <-- Displays 3 (see note below)
x; <--- x is now 4
Note: You may see the number 3 displayed on the Console instead of 4. That is just an automatic feedback from the Console. The ++ increment happens after the feedback occurs. Had we programmed it as ++x instead of x++ the increment would have happened before the automatic feedback from the Console. The end result for the variable x is 4 in both ways. We will cover this in more detail at a later lesson when we get to loops. For the most part stick to x++, it is the common way of writing it.
4- Decrement x by 1 using the unary operator --. Then call x to see the result.
x--;
x; <-- It displays 3.
5- Concatenate "Hello" to "World!”. Make sure to include a blank space between the two words.
"Hello" + " " + "World!";
or this way:
"Hello " + "World!";
or this way:
"Hello" + " World!";
6- Declare a variable named myName and assign it the first letter of your name.
var myName = "T";
7- Now add the second letter of your name to myName using the += operator.
myName += "o";
8- Do the same for all the other letters of your name. Call variable myName to see the result.
myName += "ny";
myName; <-- it displays "Tony".
Part two: Working with *, /, *=, /= and %
Clear the Console display by right clicking on it and select “Clear Console”.
Let’s work with multiplication, division and modulus:
1- Declare three variables: x = 12, y = 2 and z = 3
2- Declare another variable, a, and assign to it the multiplication of y by z. Call a to see if it holds the value 6.
3- Declare variable b and assign it the result of the division of x by a. Call b to inspect its result.
4- Reassign b to the multiplication of itself. Use *= for that effect. Now b should be 4.
5- Reassign b to the division of itself. Use /=. Now b should be 1.
6- The value of x is 12. Use the modulus operator to grab the remainder of dividing x by 2. Do not assign it to anything, just display the result. It should be zero.
7- At this moment x is still 12. Use the modulus operator to see what the remainder is when we divide x by 5. It should be 2 left.
(see the results on the next page).
Part two results (answers):
1- Declare three variables: x = 12, y = 2 and z = 3.
var x = 12;
var y = 2;
var z = 3;
2- Declare another variable, a, and assign to it the multiplication of y by z. Call a to see if it holds the value 6
var a = y * z;
a;
3- Declare variable b and assign it the result of the division of x by a. Call b to inspect its result, it should be 2.
var b = x / a;
b;
4- Reassign b to the multiplication of itself. Use *= for that effect. Now b should be 4.
b *= b;
5- Reassign b to the division of itself. Use /=. Now b should be 1.
b /= b;
6- The value of x is 12. Use the modulus operator to grab the remainder of dividing x by 2. Do not assign it to anything, just display the result. It should be zero.
x % 2;
7- At this moment x is still 12. Use the modulus operator to see what the remainder is when we divide x by 5. It should be 2 left.
x % 5;
There will be plenty more exercises to use these new skills along with other more advanced ones.
Let’s keep going forward.
END OF LAB