Рабочий код, проходит проверку. Пришлось завести еще одну переменную.
let numbers = [2, 4, 7, 4, 7, 2];
let number = 4;
let lastIndex = 0;
let num = 0;
for (let i = 0; i <= numbers.length-1; i++){
if (number === numbers[i]){
lastIndex = i;
num = numbers[i];
console.log(lastIndex)
} else if ( num !== number) {
lastIndex = -1;
}
}
let numbers = [2, 4, 7, 4, 7, 2];
let number = 4;
let lastIndex = -1;
for (let i = 0; i <= numbers.length - 1; i++) {
if (numbers[i] == number) {
lastIndex = i;
}
}
console.log(lastIndex);