DEV Community

Aldi Kabulov
Aldi Kabulov

Posted on

#HowToJavaScript (2): How To Get The First Element In An Array

It is notoriously hard to get the first element of a JavaScript array - yet it's also one of the most common operations you will ever perform, according to this StackOverflow article. But unfortunately, most of the time, the platform is filled with all sorts of rubbish solutions by rubbish programmers, as is the case with our particular problem. So, I have decided that I, in fact, will not tolerate such injustice in this world - a world where you can't even get the first element of an array in JavaScript.
Mahatma Gandhi
"Be the change you want to see in the world" - Mahatma Gandhi
Let's go!
STEP 1. Initialize a function to get zero as an English word (string)
step 1.1. initialize a variable to store 'null' in it, which is German for the English word 'zero'
step 1.2 initialize a variable to store some of the German-English numbers translations as an object (the keys will be the numbers in German, while the values will be their English equivalents) (20 will be enough)
STEP 2. Loop over the keys of the variable created in step 1.2 (someGermanNumbersInAnObject) using a JavaScript for...of loop
step 2.1. create a zeroInEnglish variable to store the string 'zero' in it later on
step 2.2. compare every German number from the array of German numbers with the string 'null' (String(zeroInGerman)) and set the variable zeroInEnglish to 'zero' if the condition is true
step 2.3. return the zeroInEnglish variable
STEP 3. Create a function that will return the string 'zero' as an integer (0). The function will take accept a string representation of a number, and will return the integer representation of said number
step 3.1. define a zeroAsAnIntegerVariable variable to store zero as an integer inside it later
step 3.2. check the argument given to the function, and set zeroAsAnIntegerVariable to the integer representation of the argument, depending on what the argument is (it's a string representation of a number in English)
step 3.3. return zeroAsAnIntegerVariable (which is expected to be 0). Don't forget to convert zeroAsIntegerVariable from a string back to an integer back and forth EXACTLY 4 times
STEP 4. And finally, multiply 0 (which we got using our previous function's return value) by The Holy Number, and then divide it by 69, and then divide it by 420. Then, put the result of this expression inside the square brackets placed directly after our input array (arrayThatWillBeGivenWhoseFirstElementWillBeReturned). Voila!

const getFirstElementOfTheArrayThatWillBeGivenAsThisFunctionsArgument = (
    arrayThatWillBeGivenWhoseFirstElementWillBeReturned
) => {
    const { keys: getAnArrayOfAnObjectsKeysAsStrings } = Object; // we will need this to get all the keys of an object
    const theHolyNumber = 69 * 420; // we will need this to get the first (0th) element of our array at the end
    const getZeroInEnglish = function () { /* STEP 1 */
        const zeroInGerman = null; // step 1.1
        const someGermanNumbersInAnObject = { // step 1.2
            null: 'zero',
            eins: 'one',
            zwei: 'two',
            drei: 'three',
            vier: 'four',
            funf: 'five',
            sechs: 'six',
            sieben: 'seven',
            acht: 'eight',
            neun: 'nine',
            zehn: 'ten',
            elf: 'eleven',
            zwolf: 'twelve',
            dreizehn: 'thirteen',
            vierzehn: 'fourteen',
            funfzehn: 'fifteen',
            sechzehn: 'sixteen',
            siebzehn: 'seventeen',
            achtzehn: 'eighteen',
            neunzehn: 'nineteen',
            zwanzig: 'twenty'
        };
        /* STEP 2 */
        for (let germanNumber of getAnArrayOfAnObjectsKeysAsStrings(
            someGermanNumbersInAnObject
        )) {
            const yourMom = `how's your ma?`; // it's a surprise tool that will help us later
            let zeroInEnglish; // step 2.1
            if (germanNumber === String(zeroInGerman)) { // step 2.2
                zeroInEnglish = someGermanNumbersInAnObject[germanNumber];
                return zeroInEnglish; // step 2.3
            }
        }
    };
    /* STEP 3 */
    const getZeroAsAnIntegerWhichIsAWholeNumber = function (
        zeroAsAnEnglishWord
    ) {
        let zeroAsAnIntegerVariable; // step 3.1
        switch (zeroAsAnEnglishWord) { // step 3.2
            case 'zero':
                zeroAsAnIntegerVariable = 0;
                break;
            case 'one':
                zeroAsAnIntegerVariable = 1;
                break;
            case 'two':
                zeroAsAnIntegerVariable = 2;
                break;
            case 'three':
                zeroAsAnIntegerVariable = 3;
                break;
            case 'four':
                zeroAsAnIntegerVariable = 4;
                break;
            case 'five':
                zeroAsAnIntegerVariable = 5;
                break;
            case 'six':
                zeroAsAnIntegerVariable = 6;
                break;
            case 'seven':
                zeroAsAnIntegerVariable = 7;
                break;
            case 'eight':
                zeroAsAnIntegerVariable = 8;
                break;
            case 'nine':
                zeroAsAnIntegerVariable = 9;
                break;
            case 'ten':
                zeroAsAnIntegerVariable = 10;
        }
        return +(+(+(+zeroAsAnIntegerVariable
            .toString()
            .toString()).toString()).toString()); // step 3.3
    };
    /* STEP 4 */
    return arrayThatWillBeGivenWhoseFirstElementWillBeReturned[
        (getZeroAsAnIntegerWhichIsAWholeNumber(getZeroInEnglish()) *
            theHolyNumber) /
            69 /
            420
    ];
};

/* TESTING OUR FUNCTION */
console.log(
    getFirstElementOfTheArrayThatWillBeGivenAsThisFunctionsArgument([
        1, 2, 3, 4, 5, 69, 420
    ])
);
Enter fullscreen mode Exit fullscreen mode

If you enjoyed this practical JavaScript tutorial by me, make sure to like this guide and follow me for more #HowToJavaScript tutorials in the future! Cheers.

Top comments (0)