How to find index of first duplicate in array in Javascript?


    const numbers = [2, 4, 5, 2, 3, 5, 1, 2, 4];

I need to create the function indexOfRepeatedValue (array). Use numbers that are stored in the variable numbers.

I should create a variable firstIndex in this function. In the for loop, check which number repeats first and assign its index to firstIndex. Then write this variable to the console – outside of the for loop.

I came up with this idea:


    function indexOfRepeatedValue(array){
        let firstIndex;
        for (let i=0; i < array.length; i++ )
            if (firstIndex.indexOf(array[i]) === -1 && array[i] !== '');
        firstIndex.push(array[i]);
        return firstIndex;
    }
    
    indexOfRepeatedValue(numbers);

It doesn’t work at all. I’m lost, some piece of advice?