DEV Community

Cover image for #7DaysJS: Where is My Element
Lautaro Lobo
Lautaro Lobo

Posted on • Originally published at lautarolobo.xyz on

1 1

#7DaysJS: Where is My Element

Welcome to day 5 of 7 Days of JavaScript! Today we will code a classic function that:

  • Takes two parameters: an element and an array of elements
  • Returns the position of the element inside the array. If the element is not in the array, returns -1.

Wish you luck!

You can see the solution here.

Top comments (2)

Collapse
 
aminnairi profile image
Amin

Recursive solution with runtime type checking.

"use strict";

/**
 * Find the index of an element in an array
 * @param {unknown} element Element to search
 * @param {unknown[]} elements Elements in which to search from
 * @param {number} position Starting position (default to 0)
 * @throws {TypeError} If the second argument is not an array
 * @throws {TypeError} If the third argument is not an integer
 * @throws {RangeError} If the third argument is lower than zero
 * @return {number} -1 if the element is not found, the index otherwise
 * @example index(1, [-1, 0, 1, 2, 3]); // 2
 * @example index(2, [-1, 0, 1, 2, 3]); // 3
 * @example index(5, [-1, 0, 1, 2, 3]); // -1
 */
function index(element, elements, position = 0) {
    if (!Array.isArray(elements)) {
        throw new TypeError("Expected second argument to be an array");
    }

    if (!Number.isInteger(position)) {
        throw new TypeError("Expected integer for the third argument");
    }

    if (position < 0) {
        throw new RangeError("Expected third argument to be greater or equal to zero");
    }

    if (elements.length === 0) {
        return -1;
    }

    /**
     * @const {unknown} current The first elements of the array
     * @const {unknown} rest All elements except the first one
     */
    const [current, ...rest] = elements;

    if (current === element) {
        return position;
    }

    return index(element, rest, position + 1);
}

console.log(index(1, [-1, 0, 1, 2, 3])); // 2
console.log(index(2, [-1, 0, 1, 2, 3])); // 3
console.log(index(5, [-1, 0, 1, 2, 3])); // -1
Collapse
 
lautarolobo profile image
Lautaro Lobo

Woah, interesting...

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay