Learn JavaScript Properly 1-3 Decisions and Loops
Beginning JavaScript Chapter 3
Make computer more intelligent by giving it decision-makeing abilitys.
What we’ll learn?
- Number or string comparison.
- Making decision with the
if
,else
, andswitch
.(answer is ether true or false).
Comparison Operators
First get some “tools” in hand: operators (used between LHS (Left Hand Side) and RHS, normaly called “Left Operand” and “Right Operand”.
Operator | Purpose |
---|---|
== | equal to |
< | less than |
> | greater than |
<= | less than or equal to |
>= | greater than or equal to |
!= | not equal to |
order of “PRECEDENCE”:==
and !=
are lowest, >
, <
, <=
, >=
and !=
are all the same.
On w3school:Javascript best practices and on 24 JavaScript best practices, they suggested not to use ==
or !=
, but use ===
and !==
. Because ==
and !=
automaticly convert the operand to compariable types, it may cause some problem sometimes.
The result of the comparison can be assign to a variable. (value will be either true or false.)
The IF Statement
Syntax will looks like:
if(TEST CONDITION) { |
It’s read like “ if
the (TEST CONDITION)
is true, then run the CODES
.”{ }
marks “block of code” in JavaScript.
Logical Operators
Operator | Function |
---|---|
&& | AND |
|| | OR |
! | NOT |
Logic!!!
Multiple conditions can be pass in to if()
statement. Ex:
if(THIS && THAT ! THAT1) { |
One simple example:
var myAge = parseInt(prompt("Your age?", ""), 10); |
else and else if
syntax:
if(CONDITION){ |
if
the “CONDITION” is “true” do “ACTION1”, or else
do “ACTION2”.
if(CONDITION1){ |
if
the “CONDITION1” is “true” do “ACTION1, else if
the “CONTITION2” is “true” do “ACTION2”, else
do “ACTION3”.
String Comparison
using same operators, but it compares all letters, and “case sensitive”.
It compares the ASCII Unicode number of each character.
To get rid of case sensitive problem, just convert string operands to all uppercase or all lowercase by using:toUpperCase()
or toLowerCase()
.
Switch
A more efficient to check for large number of possibility.
syntax:
switch(aVariable) |
- Start a
switch()
function, pass in “aVariable” that we want to check. - For the
case
of “Paul” do “ACTION1” then “ACTION2” then stop at thebreak
point. - For the
case
of “Jason” do “ACTION3” then stop. - For all other cases(default case), do “ACTION0. (
default
statement is optional, but always good to declare it.)
example is a number guessing script:
var theNumber = prompt("guess it's 1 or 2", ""); |
- the “case 0” and “case 1” are doing the same thing so we can merge it like:
case0:
case1:
alert("Too low!");
break; - this will do the same thing for “case0” and “case1”.
Looping - FOR and WHILE
Repeat the same thing when condition matched.
Using for
and while
statement.
for loop
- use the code a cetain number of times.
Syntax:This code says: Initiate the “loopCounter” to be “1”, when the “loopCounter” is less then or equal to “3”, do the “ACTION” and add “1” to “loopCounter”.for(loopCounter = 1; loopCounter <= 3; loopCounter += 1)
{
ACTION;
}
(number of iteration.)
Converter example:
var degFahren = [212, 32, -459.15]; |
- First declare Variables.
- Loop through the array “degFahren”, convert the value and sotre it into “degCent”.
- Loop “BACK” and print out the values.
The “for…in” Loop
Use it to loop throug all items in an array (without knowing the exact number of the items.)
syntax:
var arrayIndex; |
- “arrayIndex” is a variable which will automatically be populated with/assign to the next index value in the array.
- This code will loop through all items in the array “arrayName” and each time put the index number into “arrayIndex”.
Ex: (two ways to loop througe var myArray = ["Paul", "Jason", "NOOB"];
)
var loopCount; |
or
var loopCount; |
It just became shorter and looks cleaner!
The while Loop
The differences between for
and while
:
for
used for looping a certain number of times,while
enables you to test a condition and keep looping while it’s true.for
: when you know how many times you need to loop.while
: when you don’t know.
(BUT ACTUALLY I JUST CAN’T SEE ANY “TRUE” DIFFERENCE……)
syntax:
while (CONDITION) { |
It can also do the same work as if
by writing:
while (loopCount < x){ |
This will loop through the “ACTION” “x” times.
If you forget to put loopCount += 1;
, the code will be an infinite loop.
The do…while loop
syntax:
do { |
Use this if you want to run the action first and check if the condition is true, if yes, loop through the action.
But it’s rarely used, so try to avoid it unless really necessary.
break and *continue
break
can be used in switch
statement, but also can be used to left a for
or while
loop.
Example:
var theNumber; |
- In this example, the script will alert “Put in a NUMBER please” and
for
loop will be stoped if user did not put in a number.
var theNumber; |
- This example makes the code more interactive by replace the
alert
byprompt
, and changebreak
tocontinue
.