I learn JavaScript - Part 3 - Conditions and LoopssteemCreated with Sketch.

in #programming8 years ago

After I covered in my first two posts Variables, Statements, Functions and Objects, I will share in this post what I have learned about Loops and Arrays and how I will use all the above to code my first little game going forward.

Condition - If ... else

Let's start with the following example of an If... else statement.

if (steemPrice < .10) {
    BUYsteemLikeCrazy();
    } else {
    waitForIt(); 
    }

So what does this mean, let's break it down.

The first statement defines the condition (steemPrice < .10). If this condition is true than the next line is executed which in this case is a function called BuysteemLikeCrazy.

If the condition (steemPrice < .10) is not true or false, than we skip the BUYsteem method and jump to the second method under the else statement waitForIt().

You see condition statements are extremely useful. We could extended the conditions with more else if statements that can be executed if we have more conditions to check.

if (condition1) {
  Executed if condition1 is true
  } else if (condition2) {
  Executed if the condition1 is false and condition2 is true
  } else {
  Executed if the condition1 and condition2 are false
  }




Loops

Using Loops can safe you a lot of time if you need to run a code repeatedly.
Let's say you want to print the numbers 0 to 9 on your screen. One way to achieve this is by writing the following lines 10 times.

console.log(0);
console.log(1);
console.log(2);
console.log(3);
.
.
console.log(9);

To do the same task more effectively we can use the for loop in JavaScript. The code for printing the number's 0 to 9 would look like this.

for (var i = 0; i < 10; i++) {
 console.log('Number ' + i);
 }

Let's break it down again.

  • We declare a variable called i and give it a start value of 0.
  • The loop will run until the value of i is less than 10.
  • Finally we want to increase the value by one each time the loop run.
  • On the next line we print out the word Number and the number on the screen.

The output looks like this.

Number 0
Number 1
Number 2
Number 3
.
.
.
Number 9

Another loop to achieve above task is by using the while loop.

The code would look like this.

while (i < 10) {
 console.log('Number ' + i);
 i++;
 }

In the while loop we don't declare the variable (i) but just state the condition. The variable (i) would need to be declared and a start value assigned outside of the while loop.

My next plans from here.

Now that I have learned the very basics of JS I plan to code a little browser game deepen my understanding. I will start to upload my files on GitHub so you can follow my progress if you are interested.

100% Power up.



Sort:  

You need IQ to learn programming
But it's good to have a big EQ to explain so well what we learn
Keep on :)

Thank you very much @charlie777pt. I will definitely continue to share the progress I made.

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.027
BTC 58974.35
ETH 2687.14
USDT 1.00
SBD 2.50