Tech Junkie Blog

Latest Posts

Monday, June 18, 2018

The indexOf() method searches for the element in the array that matches the passed in value.
In this example we will search for the value 60.50 in the array.  The indexOf() method takes two arguments.  The first argument is the value to search for, the second argument is optional and specifies the index to start the search at.  If the second argument is omitted the search will start at the first element of the array.

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

        var searchAtBeginning = oilPrices.indexOf(60.50);

        var searchAtIndex = oilPrices.indexOf(60.50,2);

        console.log("searchAtBeginning: " + searchAtBeginning);
        console.log("searchAtIndex: " + searchAtIndex);
    </script>

Here is the output:







As you can see the first indexOf() method call returns the index of 1 because that's where 60.50 resides.  It was able to find the element because the second argument was omitted there the search starts at the beginning array.  The second indexOf() method call has the value of 2 as the second argument that tells the method start the search at index 2 in the array.  Since the value 60.50 exists at index 1, the method returns a -1 which means it cannot find the value.

Monday, June 11, 2018

The some() array works similar to the every() method, it also returns true or false when the condition is met.  some() needs to match just one element in the array that meets the condition to return true.  So lets run the same code that we ran for the every() method.  We will test to see if the array is greater than 50 for the first condition, and then we will test to see if the array is greater than 70 for the second condition.  For the every() method the first condition returns true, while the second condition returns false.  Let's see what happens with the some() method.


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

        var greaterThanFifty = oilPrices.some(function (value) { return value > 50; });

        var greaterThanSeventy = oilPrices.some(function (value) { return value > 70 });

        console.log("greaterThanFifty: " + greaterThanFifty);
        console.log("greaterThanSeventy: " + greaterThanSeventy);
    </script>







As you can see both conditions return true because at least one of the element in the array meets the condition.

Monday, June 4, 2018

The every() method is a method that tests a condition on every array element and makes sure that all the elements meets the criteria.  It returns true or false.

The code below test to see if the oil prices array is greater than $50 or greater than $70


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

        var greaterThanFifty = oilPrices.every(function (value) { return value > 50; });

        var greaterThanSeventy = oilPrices.every(function (value) { return value > 70 });

        console.log("greaterThanFifty: " + greaterThanFifty);
        console.log("greaterThanSeventy: " + greaterThanSeventy);
    </script>







As you can see the first condition is true because all of the elements in the array are greater than 50.  However, the second condition returns false because 69.50 is not greater than 70 even though the rest of the elements in the array are greater than 70.

Monday, May 28, 2018

The filter() method returns the elements of the array that satisfies the function that is passed into the method.  If the function returns true the element is added to the filtered array, else it is ignored.

Let's say we only want to buy oil when the prices drops below $72, so we will only return an array element where the prices below $72


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

        var lowPrices = oilPrices.filter(function (value) { return value < 72 });

        console.log(lowPrices);
    </script>


Monday, May 21, 2018

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>


The code above calls the map() method on the oil prices and returns a discounted value of $1 for each array element


Thursday, May 17, 2018

R is a programming language that is a little different in syntax then your typical mainstream languages like Java or C#. In this post we will go over how to create variables in R.

Let's create some variables about a user:

To create a variable in we use the <- key for example if you want to assign a value to variable firstName, you would write firstName <-  "John" or "John" -> firstName










If you type in the firstName in the console you will "John" as the value









To get the type of the variable you can use the class(firstName)