const numbers = [2, 4, 5, 2, 3, 5, 1, 2, 4];
I’m looking to write a function called indexOfRepeatedValue (array) that utilizes the numbers stored in the provided variable. The function should create a variable called firstIndex. Within a for loop, determine which number repeats first and assign its index to firstIndex. Finally, log this variable to the console beyond the for loop.
This initial idea isn’t working as intended. I could use some guidance or advice on how to proceed. Any help would be greatly appreciated!
const numbers = [2, 4, 5, 2, 3, 5, 1, 2, 4];
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;
}
console.log(
indexOfRepeatedValue(numbers)
)