Thursday, 3 July 2014

Chapter 11: JavaScript Loops

JavaScript Loops
Very quickly we’ll find ourselves writing code that we want to have happen multiple times. Sometimes when we want to change background color, hiding everything in a menu, we’ll realise that this code we want repeated.
The main issues what any loop, is not when to loop, looping is easy, it’s when to stop.

While Loop
So let’s first fig rout the most basic kind of loop. Well we’ve seen if statements before. And this is all we have here. If this conditions is true in the parentheses we’ll execute the code inside the curly braces. All we do here is to replace the if with the word while, we have a loop. Whatever is in the block get done every time the loop goes around.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>
var a = 0;
while ( a < 9) {
console.log(a);
a++;
}                       


</script>
</body>
</html>
                        

In the above example we ask is a less than 9, and return true. Without the increment a++ at the end of the body of the loop we create infinite loop. So the loops will start increment every time we go around the loop and we’ll keep on going.
There’s another variant of the while loop called do…while loop. This keep the same basic format, but we actually move the condition to the end. So instead of while a is less than 10 been at the start of the block is actually at the end of the block, because of that we actually need a semi-colon at the end of the do…while loop.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>
var a = 1;

do {
console.log(a)
a++;
} while ( a < 10);



</script>
</body>
</html>


So let you know. while loops are much more common than these do..while loop. In most loops is expected to check the condition before entering the loop, and it’s more readable that way too.


for Loop           
With the for loop you actually bring three pieces together inside the parentheses they all there at the top. First we set up the index, so in this case var i = 1, it follows semi-colon, than we check the condition is less than 10, than even it’s right at the top this is the incrementer, this happens at the end of every loop. We don’t have to go by one. You could go up by 4, 3, 10 or 500 if you wanted. So this is very readable, because everything about the loop is right there at the top, you don’t have to look outside the loop for the index or the increment. So for loop is very common way of doing this.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>
var a = 0;
for (i=0; a<10; a++) {
                                    console.log(a);
}

</script>
</body>
</html>



Break
Break is a word that will jump us out of the loop. Inside the for we have if statement for example and say is a equal to 25, no it isn’t. So will skip passed that code block and continue on hit the end of the loop go back up, check condition again, yes I is still less than 3000, continue on till i is equal to 25 and manually hit break. As soon as we hit break we basically manually say we are done, we jump completely out of the loop and we can continue on.
If we say break we are completely done with the loop.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>
for ( i = 0; i < 3000; i++ ) {
                                    console.log(i);
if ( i == 25) {
break;
}

}


</script>
</body>
</html>



Continue
The word continue doesn’t mean jump out of the loop, it means jump back up and check the condition again. Don’t continue and further with this iteration just this one. We are not done with the entire loop but we are done with this time around.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>

for ( i = 0; i < 99; i++ ) {
                                    console.log(i);
if ( i == 12) {
continue;
}
// do stuff

}


</script>
</body>

</html>

Chapter 10: Operators - Ternary

Ternary
Finally the last one is the Ternary operator. The general format of this is that you have a condition, you asking something, you say what happened of this is true and what happens if this is false. You can actually think of it as a many if else statements. So let me show you a example here:

Syntax:
Condition ? true : false


Default example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>
var player1 = 400;
var player2 = 495;
var winner;

if (player1 < player2) {
            console.log("player2 is winner");
} else {
            console.log("player1 is winner");
}

</script>
</body>
</html>

Ternary Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<script>
var player1 = 400;
var player2 = 495;
var winner;

( player1 < player2 ) ? console.log("player2 is winner") : console.log("player1 is winner") ;

</script>
</body>

</html>

Chapter 9: JavaScript Modulus / Increment / Decrement

Modulus

In JavaScript we have modulus or remainder operator. The one is little different. Probably the classic example is using it to do something like calculating a year. So in this case I create a variable called year that is equal to 2003 and I can’t variable named remainder and I set that equal to year % 4. The Percent here is modulus or remainder operator. What it means is divide the variable year ( 2003) by 4. This don’t give me the result. Give me the remainder. In this case 4 will go to year 500 times, and the remainder would be 3.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var year = 2003;
var reminder = year%4;

console.log(reminder);


</script>
</body>
</html>




Increment and Decrement
Next we have shorthand for adding to a variable. We already saw this but let’s explain in wider.
How you know we can use this:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;

console.log(a = a + 1);

</script>
</body>
</html>
or we can use that:

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;

console.log(a += 1);

</script>
</body>
</html>

OR

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var increment = ++a;

console.log(increment);

</script>
</body>
</html>

The result of all would be variable a + 1;

The above example is used to increment the variable a.
But we can use it also to decrement it.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var b = 20;
var c = 30;

console.log(--a);
console.log(b = b - 1);
console.log(c -= 1);

</script>
</body>

</html>

Chapter 8: JavaScript Operators

JavaScript Operators
Just about every statement we’ll write is going to be involved an operation. To perform lots of this operations we need operators. That means the symbols we use to manipulate a values.
The most obvious are probably the arithmetic operators.
+
Addition
-
Subtraction
*
Multiplication
/
Division

The equal sign is the most used operator, called assignment operator.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var b = 5;

console.log(a+b);
console.log(a*b);
console.log(a-b);
console.log(a/b);

</script>
</body>
</html>


You might see something like this:
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;

console.log(a = a + 1);

</script>
</body>
</html>



In this example to variable score is added 1. But there’s shorthand for this by adding + sign before the equal sign. Both examples are similar.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;

console.log(a += 1);

</script>
</body>
</html>


Another ways:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;

console.log(a += 2);
console.log(a -= 2);
console.log(a *= 2);
console.log(a /= 2);


</script>
</body>
</html>



Multiplication and division is more important in JavaScript so when you have code like that:
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10 + 5*5;

console.log(a);


</script>
</body>
</html>



Comparison operator
The equal sign is the comparison operator in JavaScript.
The single equal sign is a command.
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var b = 20;

if (a = b) {
            console.log("This will alwasy return true.");
}


</script>
</body>
</html>



The above example will always return true because we set an equal to the value of b, than this return true and the code in the block will be always executed.
So remember:
One equal sign is assignment.
Two equal sign are the equality operator, you are asking.
Three equal signs is strict equality operator.


Comparisons:
If ( a == b) { …       Checking equality
If ( a != b) { …       Not equal to
If ( a === b) { …    Strict equality
If ( a !== b) { …    Not strictly equal to
If ( a > b) { …       Greater than
If ( a < b) { …        Less than
If ( a >= b) { …     Greater than or equal to
If ( a <= b) { …     Less than or equal to



Sometimes we want to know if a is strictly equal to be and and c is strictly equal to d we do that:
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var b = 10;
var c = 30;
var d = 30;

if (a === b && c === d) {
            console.log("a is equal to be and c is equal to d");
}


</script>
</body>
</html>

OR

<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var b = 10;
var c = 30;
var d = 30;

if ((a === b) && (c === d)) {
            console.log("a is equal to be and c is equal to d");
}


</script>
</body>
</html>


The above example uses double &, which means and.



What if we are interested one OR the other situation. If the variable a is strictly equal to be OR if the variable c is strictly equal to d. We use the double vertical bar.
Example:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<div id="demo">
<h1>This is a heading.</h1>
<p>This is a paragraph.</p>
</div>
<script>
var a = 10;
var b = 10;
var c = "30";
var d = 30;

if ((a === b) || (c === d)) {
            console.log("The code block will be executed even if one of the conditions return true.");
}


</script>
</body>

</html>