Stwórz funkcję indexOfRepeatedValue(array). Prześlij do niej tablicę z 10 liczbami które są zapisane w zmiennej numbers.
Stwórz w tej funkcji zmienną firstIndex. W pętli for sprawdź, która z liczb powtarza się jako pierwsza i przypisz jej indeks do zmiennej firstIndex. Następnie wypisz w konsoli tą zmienną – poza pętlą for. Zwróć wartość firstIndex z funkcji. const numbers = [2, 4, 5, 2, 3, 5, 1, 2, 4];
mój kod nie działa bo totalnie nie wiem jak wypisać to w funkcji:
return firstIndex;
console.log(firstIndex);
a to cały mój kod:
function indexOfRepeatedValue(array) {
let firstIndex = "";
for (let i = 0; i < numbers.length; i++) {
for (let j = i + 1; j < numbers.length; j++) {
if (numbers[i] === numbers[j]) {
if (firstIndex === "") {
firstIndex = array.indexOf(numbers[i]);
break;
}
}
}
if (firstIndex !== "") {
break;
}
}
return firstIndex;
console.log(firstIndex);
}
indexOfRepeatedValue(numbers);
pytanie nowicjusza wiec proszę o wyrozumiałość