My task is:
- To develop a function named,
indexOfRepeatedValue(arr)
, - To establish a
firstIndex
variable within the function, - To utilize a
for
loop to identify the first repeated number in a given array, - And to store the index of that number in the created
firstIndex
variable
The provided array is as follows:
const givenArr = [2, 4, 5, 2, 3, 5, 1, 2, 4];
For this array, the first replicated number is 2. Therefore, the value of the firstIndex
variable should be 0 (upon returning the value from the function).
My code progression is shown below,
const givenArr = [2, 4, 5, 2, 3, 5, 1, 2, 4];
function indexOfRepeatedValue(arr) {
let firstIndex;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === ) { //The area where I am missing a crucial element
firstIndex = ;
}
break;
}
return firstIndex;
}
console.log(indexOfRepeatedValue(givenArr));
I am uncertain of the correct value to include in the if
statement. I am unable to use the indexOf
function at this time. Any guidance would be greatly appreciated as I am eager to learn.