Unit6-Array_lastIndexOf


// IndexOf() 從第一個開始找遇到的第一個 INDEX
// lastIndexOf() 從後面找回來遇到第一個 index

function lastIndexOf(arr, target) {
for(let i = arr.length -1; i >=0; i–) {
// console.log(arr[i]);
if (arr[i] === target) {
return i
}
}
return -1
}

console.log(lastIndexOf([2,1,3]))


Author: Jayson-Zheng
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Jayson-Zheng !
  TOC