Tech Junkie Blog - Real World Tutorials, Happy Coding!: November 2021

Tuesday, November 30, 2021

Since JavaScript functions are objects they can have properties that belongs to the function itself.  For example this simple example counts the number of times the function is being called by incrementing the counter property that belongs to the function.


    <script>
        functionCount.counter = 0;

        function functionCount() {
            return functionCount.counter++;
        }

        for (var i = 0; i < 10; i++)
            functionCount();

        console.log(functionCount.counter);

    </script>

Monday, November 29, 2021

When you define an object as an Array in JavaScript you get the benefits of inheriting some useful methods with it.  Some of the common methods are those that allows you to add and delete elements from an array.  The .push() method adds an element at the end of the array, the unshift() method adds an element at the beginning of the array.  The delete keyword deletes an element at the specified index.

Let say you have an the following array:

var numbers = [3,4];

If you output the array in the browser console you will see that the array has two elements with the number 3 occupying index 0 and the number 4 occupying index 1.

Friday, November 26, 2021

The switch statement is there to make the code more readable if the if statements contains too many branches and becomes too hard to read.

Consider the code below

var x = 6;

if (x === 1)
{
     console.log("one");
}
else if (x === 2)
{
     console.log("two");
}
else if (x === 3)
{
     console.log("three");
}
else if (x === 4)
{
     console.log("four");
}
else if (x === 5)
{
     console.log("five");
}
else
{
     console.log("x is not 1-5");
}

Thursday, November 25, 2021

Since an array is a collection of elements of objects, values, etc.  The most common function performed on it is to iterate through each elements of the array.  The most frequent way to do that is with a for loop.

Let's say you have an array with the following:

var numbers = ["one","two","three","four","five"];

The numbers array above contains five string elements, which also has a indexes associated with the it.  There's also a length that is defined with each array that we will use for the for loop we are about to write.  So what do the you length of the numbers array will be?

If we output the array to the browser with the statement console.log(numbers.length) do you think we will get the length to be 5 or 4?

If you guess 5 you are correct because there are five elements in the array.  However, the arrays have zero based indexes therefore the index is always length -1

for(var i = 0; i < numbers.length; i++)
{
     console.log(numbers[i]);
}

Wednesday, November 24, 2021

JavaScript itself does not have multidimensional arrays, instead you can use an arrays of arrays instead.  Multidimensional arrays are basically arrays that you can reference by rows and columns.  The most obvious real world use is to store rows and columns of a database record.  Imagine if you have a row of the database product from the database from the table "Products".  Instead of storing each value in an individual variable you can store them in an arrays of arrays.

Let's say you have the following database Products table

Tuesday, November 23, 2021

In the previous post we installed MongoDB on Linux on this post we are going to install MongoDB on Windows.

Here are the steps:

1. Go to https://www.mongodb.com/download-center/community, then click on the "Download" button





















Monday, November 22, 2021

The Array.reverse() method reverses the order of the array elements.  The easiest way to demonstrate this is to have an array with the numbers 1 to 5.  If we call the Array.reverse() method we should get an array of 5 to 1.  The Array.reverse() does not create a new array, it simply rearranges the array into a new order.  Just keep that in mind when you are using this method.

So let's declare our array from 1 to 5


    <script>
        var numbers = [1, 2, 3, 4, 5];
    </script>

So if we call the numbers.reverse() method we should get an array of 5 to one in the reverse order

Friday, November 19, 2021

The for loop is a looping construct in JavaScript that are more simplistic than the while loop.   When writing the for loop there are three three parts they are


  1.  Initialize - a count variable is initialize, it's usually zero but it doesn't have to be
  2. Test - A test to see if the loop should continue
  3. Increment - finally the count is incremented, it doesn't have to be an increment it could be decrements as well

Let's use the JavaScript code to demonstrate:

If you execute the loop below you will bet a for loop that executes when i is less than 10


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

console.log("i outside of for look is " + i);

Thursday, November 18, 2021

The forEach() method loops through an array, and performs a function on each element of the array.  The forEach() method can take three arguments, the first argument is a current value that you want to perform an action on.  The second argument acts as the "this" value within the scope of the function.  The third argument is the array itself.  Most of the time only the first argument is passed.  The code below passes in only one argument which is an anonymous function being performed on each element of the array using the forEach() method to calculate the oil prices in the last five days and prints it out in the console.


    <script>
        var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99];
        var total = 0;

        oilPrices.forEach(function (value) { total += value });

        console.log(total);
    </script>

Wednesday, November 17, 2021

 In the previous post we created a load balancers with the target groups, rules, and instances to handle HTTPS traffic.  In this post we are going to set up our DNS records in Route 53. So go to the Route 53 dashboard, click on your domain

1. Click on "Create record"



Tuesday, November 16, 2021

In this post we are going to go over some basic commands that you can run on a machine that has MongoDB installed.

One of the first thing you want to do is to connect to MongoDB so that you can run more commands.
Here is the command to connect to MongoDB

mongo --host=localhost --port=27017

All you need is the name of the host and port number, 27017 is the default port number and since we running the command on the MongoDB host, the host is localhost
















Monday, November 15, 2021

The map() method is similar forEach() method which means that it performs a function, however it returns a new array with the returned values.  In this example we are going to perform the $1 discount of the oil prices using the map() method instead of the forEach() the difference here is that the map() method returns a new array instead of the original array, but the result will be the same.


    <script>
        var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99];

        var discounted = oilPrices.map(function (value) { return value -1; });

        console.log(discounted);
    </script>


Friday, November 12, 2021

The conditional ?: operator in JavaScript has three operands the first part goes before ? the second goes between the ? and the : symbol and the third operand goes after the : symbol.  You can think of this operator as shorthand way of writing a an if statement.  The first operand evaluates to a boolean, if the first operand is true then evaluate the second operand else evaluate the third operand.

Let's say we want to want to see what the gender of the baby is and assign it a name, let's demonstrate it with code.

var gender = "girl";

var name = gender === "boy" ? "Julian" : "Julie";

console.log(name);

The code above assigns the variable gender to "girl" then it uses the conditional ?: to assign a value to the variable name if the gender is "boy" then assign the name "Julian" to name, else assign the name "Julie"

Thursday, November 11, 2021

JavaScript error handling is a lot like other languages where you have the try/catch/finally  statement that you can used.  The basic premise is

try
{
     // try some codes here fingers crossed that nothing bad happens
}
catch(ex)
{
     // oops something bad happened, this is where it gets handled or I tell the user
}
finally
{
    // I don't care what happens I am going to execute and get paid
}

Here is the error handling in action:

Wednesday, November 10, 2021

 In the previous post we created four instances with a Launch Template. In this post we are going to add DNS records in Route 53 and configure our Application Load Balancer with our certificates.

Before we start creating stuff let's take a step back and look at how we want to configure the website.  Let's say a bank wants to branch out into investing, so it wants to dedicate to instances to it's investing arm.  In our architecture we would have two target groups, one target group handling traffic for https://acmebanking.com and the other target group handling traffic for https://investing.acmebanking.com

We are going to register all four instances on the load balancer.

1. So now we ready to create an Application Load Balancer, give it a name and for the listener add an HTTPS listener to the existing one


Monday, November 8, 2021

The most common ways you see how objects are created in JavaScript is the object literal syntax.  The object literal syntax is the open { } with comma separated name: pair value lists.  The name: part is the name of the property.  The part after : is the property value which can hold a primitive value or another object.  It could even be a function.

Here is an example of an object literal:

var product = {
    name: "Chai",
    Category: "Tea",
    Country: "India",
    supplier: {
        name: "ACME Tea Of India",
        location : "New Delhi"
    },
    related: ["Earl Grey", "Green Tea", "Dark Tea", "White Tea"],
    display: function () {
        console.log(this);
    }
};

Friday, November 5, 2021

The eval() function in JavaScript provides the power of dynamic evaluation, it evaluates a strings of JavaScript codes and returns a value.  If you use the eval on pair of string numbers it will be forgiving and give you the number if it can.  For example if you type int

eval("4+5")  the result will be 9 even though it's a string

Thursday, November 4, 2021

In the previous post we visited how to create an object with an object literal.  In this post we are going to create the same object using the new keyword.  When creating objects using the new keyword it is required that the it is followed by a function call.  The purpose of this function call is the needs a way to call to the constructor of the new object.  A constructor is used to create or initialize the created object.

Here is the code to rewrite our literal object using the new keyword:

var product = new Object();

product.name = "Chai";
product.category= "Tea";
product.country= "India";
product.supplier= {
    name: "ACME Tea Of India",
    location: "New Delhi"
};

product.related = new Array("Earl Grey", "Green Tea", "Dark Tea", "White Tea");
product.display = function () {
    console.log(this);
};

Wednesday, November 3, 2021

 In the previous post we created our certificates in the Certificate Manager, in this post we are going to create four instances using a Launch Template so that we could use it in our load balancer.  

1. We are actually going to use "Launch Template" to create our instances, so click on "Launch Templates" under "Instances" in the EC2 Dashboard

2.  Click on the "Create launch template" button
3. Give your template a name, then scroll down to AMI type


Tuesday, November 2, 2021

In this post we are going to install MongoDB on a Linux operating system.

Here are the steps:

1.  Download the community edition of MongoDB from the page https://www.mongodb.com/download-center?jmp=nav#community click on the "Linux" tab is selected and select the Linux version that you want

Monday, November 1, 2021

Object.Create( ) is a static function which can take two arguments.  The first argument is the prototype of the object, while the second argument is the properties of the argument.  A prototype is a second object that is created with every JavaScript object.  It is like a blueprint, or the model that the object is built on.  Think of it as a car prototype.

So let's create an object with Object.Create( ) function:

var person = Object.create({
    name: "Tech Junkie",
    location: "Mars",
    hobbie: "Video Games"
});

console.log(person);

Search This Blog