javascript

findElementIndex()

Parameters: Array array, Object element

Accepts an array and an element to find

Returns: Returns index of element, returns -1 if not found

The findElementIndex function checks an array for a specific element and returns the index if found. The indexOf method is used to perform this function, and -1 is returned if the element is not found.

functions
variables
arrays
indexOf
Medium dificulty

Understanding Array Indexing in JavaScript: The find-element-index Function

Hello, esteemed programmer! Welcome to our blog post where a cool and clear learning experience awaits you. We aim to provide a straightforward and jargon-free exploration of how to program a 'findElementIndex' function in JavaScript. Stay tuned for the steps below. Let's decode this function together whilst ensuring our understanding remains as robust as our coding. Remember, every bit of code improves the larger picture, just like in a puzzle, every piece contributes to the final image.

Step 1: Understand the Problem

The first step, as with any coding task, is to understand the problem. We want to write a javascript function, called findElementIndex, which takes in an array and an element as its parameters. The purpose of this function is to find the index of the given element in the array. If the element is not in the array, the function should return -1.

Step 2: Identify the Inputs and Outputs

    let findElementIndex = (arr, element) => {
    }

Here, arr is the input array and element is the element whose index we are trying to find in the array. The function’s output is either the index of the element in the array or -1 if the element is not present.

Step 3: Write a Loop to Traverse the Array

Write a for loop to traverse the array and check each element if it is the same as the given element.

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

    }

Inside the loop, we can use if statement to compare the current array element with the given element.

Step 4: Compare the Element

Inside the for loop, write an if statement to check if array element is the same with the given element. If it is, return the current index i.

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == element){
            return i;
        }
    }

Step 5: Handling if the Element is Not Found

If the for loop completes without finding the element, this means the element is not in the array. In this case, return -1.

    for(let i = 0; i < arr.length; i++){
        if(arr[i] == element){
            return i;
        }
    }
    return -1;

So, the full function now looks like this.

    let findElementIndex = (arr, element) => {
        for(let i = 0; i < arr.length; i++){
            if(arr[i] == element){
                return i;
            }
        }
        return -1;
    }

Conclusion

Now you have a function that accepts an array and an element as input, and returns either the index of the element in the array or -1 if the element is not present. You can now use this function to find an element's position in an array. Let’s look at a quick example:

    let arr = [5, 3, 9, 7];
    let element = 9;
    console.log(findElementIndex(arr, element)); // This will output: 2

In conclusion, this is a useful function to use when you need to find the position of a specific element in an array. Just keep in mind that the function returns -1 if the element is not found in the array.

Learn function in:

Array Indexing

Retrieving the position of an element in an array

Learn more

Mathematical principle

The findElementIndex function is a practical implementation of linear search, which is a basic search algorithm that checks each element in a list sequentially until the desired element is found or all elements have been checked. This can be illustrated as: `list[0], list[1], ..., list[n]` until `list[i] == element` or `i == n`.

Learn more