Practicing with JavaScript Math Methods

Here’s a cool way to test your knowledge on Math.random, ceil, floor and round:

http://bit.ly/1xwdJz4

Have fun!

Tony

Advertisements

JavaScript value and reference variables

Are you still puzzled by JavaScript terms such as typeof,
reference variables, value variables, stack vs heap?
My new article at JSPLAIN (JavaScript in Plain Language)
is short, sweet and right to the point.
Here’s my link: http://bit.ly/1wz2rLq
Have a great weekend!!
Tony

Finding the smallest factor of a number with JavaScript

Time to practice some code!

Create a function named smallestFactor that finds and displays the smallest factor of any number (num).

Then call the function by passing in the value of  100. It should display the number 2 as a result.

Whenever you’re ready to look at my examples please refer to
http://bit.ly/1sDxC2O

Tony

A counter for a for loop in JavaScript is not essential

Did you know that a conditional expression controlling a FOR LOOP does not have to be a counter?
That’s right, all it needs to be is a Boolean expression of any kind.
Take the following loop as an example:

var finish = true;

for (var i = 0; finish === true; i++) {

    if (i >= 5) {
        finish = false;
    }

    console.log(i);
}

// result: 0,1,2,3,4,5

Variable finish is originally true and as long as it is true the for loop will run just like a while loop would.
Then we can control the loop at any time by switching the value of finish to false as it was done by the conditional statement.

READ MORE HERE: http://bit.ly/1BWiSEI

Dot Syntax

What is the meaning of the dot?
The dot signifies that we want to use something that’s inside something else.

console.log("log is inside of a console");

myObject.search("Tony de Araujo");
// search is a method which is inside of object myObject

Hope it helps!

Get your six lucky numbers with AngularJS

Hi dear reader,
If you have read my eBook Small Projects to Learn JavaScript – Drawing Six ‘Lucky’ Numbers
you’ll be pleased to know that I have converted the project into AngularJS.

AngularJS was not even mentioned in the book but I figured it was time to demonstrate a way of implementing AngularJS on the script and it is a cool implementation as you may see on the following link:

JSplain.com

Happy coding!

Tony

Code separation and encapsulation

In order to avoid conflicts of interests it is always a good idea to keep variable names unique.

Avoiding name duplication is easy to do in a simple demo script but in real life it is not so easy. Actually it is virtually impossible because code comes from different sources, the ones we write ourselves and the ones provided by applications we link to.

What we can do to prevent such conflict of interest is to isolate our own code into a closed circuit. This type of implementation if called encapsulation.

There are many ways of applying encapsulation. One such way is to include our code inside of a function. Another way is to write it as an object.

  • A function has its own scope which protects its declared assets from being mistaken by global items with the same name.
  • As for items inside of objects, they require proper addressing whenever we want to use them. That in itself prevents us from calling the wrong global item by mistake.

I show a few simple encapsulation examples on my latest post at jsplain.com.