Sunday 23 August 2020

JavaScript array operations, the hard way (Part 2/2)

Adding elements to an array, even manually, wasn't all that hard in the final analysis. But deleting... now that was a whole new kettle of fish.

Deleting elements

Again, it could have all been accomplished by use of the splice() method. Say, I wanted to delete "mamba" from the new array.
var snakes = ["cobra", "viper", "mamba", "adder", "python", "aconada"];

snakes.splice(2, 1);


Making the required array element blank would be easy. But the trick here was ensuring that the total number of elements was reduced by one! I won't go through everything I tried in order to produce the delFromArray() function, but here's what I finally did.
var snakes = ["cobra", "viper", "mamba", "adder", "python", "aconada"];

snakes = delFromArray(snakes, 2);

function delFromArray(arr, pos)
{

}


First, I declared temp as an empty array, and returned it.
function delFromArray(arr, pos)
{
    var temp = [];

    return temp;
}


Then I checked for an edge case. If pos wasn't a valid position within the array, I simply made sure the original array was returned.
function delFromArray(arr, pos)
{
    var temp = [];

    if (pos < arr.length && pos > arr.length)
    {
        temp = arr;
    }

    return temp;
}


And then there was an Else block to handle the other cases. First, I declared j and set it to -1. Then I created a For loop to iterate through the contents of arr.
function delFromArray(arr, pos)
{
    var temp = [];

    if (pos < arr.length && pos > arr.length)
    {
        temp = arr;
    }
    else
    {
        var j = -1;

        for (var i = 0; i < arr.length; i++)
        {

        }  
    }

    return temp;
}


With each iteration, I'd increment j, then set the current element of temp pointed to by i, to the element in arr pointed to by j. The idea was to transfer all elements, except for the element to be deleted, from arr to temp.
function delFromArray(arr, pos)
{
    var temp = [];

    if (pos < arr.length && pos > arr.length)
    {
        temp = arr;
    }
    else
    {
        var j = -1;

        for (var i = 0; i < arr.length; i++)
        {
            j++;

            temp[i] = arr[j];
        }  
    }
   
    return temp;
}


Here, I checked if j was equal to pos after incrementing. If it was, I incremented it again. The net effect of this was that arr would skip that position in j to be transferred.
function delFromArray(arr, pos)
{
    var temp = [];

    if (pos < arr.length && pos > arr.length)
    {
        temp = arr;
    }
    else
    {
        var j = -1;

        for (var i = 0; i < arr.length; i++)
        {
            j++;

            if (i == pos)
            {
                j++;
            }

            temp[i] = arr[j];
        }  
    }
   
    return temp;
}


And of course, I had to cater for one more case. If j, after being incremented, inevitably arrived at an index that was not valid for arr, the transfer would not take place.
function delFromArray(arr, pos)
{
    var temp = [];

    if (pos < arr.length && pos > arr.length)
    {
        temp = arr;
    }
    else
    {
        var j = -1;

        for (var i = 0; i < arr.length; i++)
        {
            j++;

            if (i == pos)
            {
                j++;
            }

            if (j <= arr.length - 1)
            {
                temp[i] = arr[j];
            }
        }  
    }
   
    return temp;
}


And yes, it worked!
["cobra", "viper", "adder", "python", "aconada"];


Looks so simple now, but I had to take a while to get my head around all of it.

What was the point of this, again?

Why did I write this long-rambling story about how much of a noob I was? Well, see, the point was to illustrate how even the simplest things aren't all that simple. People take it for granted today - functions to add and delete elements from arrays are ubiquitous in almost every language I've written in - JavaScript (obviously), Ruby, PHP and so on. But what goes on behind those function calls is a lot of logic.

And while I pretty much suffered for nothing back then, here's hoping you can learn something from this, even if it's only "don't be a hardworking idiot".

Thanksssssss for reading!
T___T

No comments:

Post a Comment