-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastIndexOf.js
33 lines (32 loc) · 990 Bytes
/
lastIndexOf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
let lastIndexOf = function (array, value) {
let output = 0;
let i = array.length - 1;
while (i >= 0) {
if (array[i] === value) {
return i;
}
i--;
}
output = -1;
return output;
}
console.log(lastIndexOf([0, 1, 4, 1, 2], 1), "=?", 3);
console.log(lastIndexOf([0, 1, 4, 1, 2], 2), "=?", 4);
console.log(lastIndexOf([0, 1, 4, 1, 2], 3), "=?", -1);
console.log(lastIndexOf([5, 5, 5], 5), "=?", 2);
console.log(lastIndexOf([], 3), "=?", -1);
// student original code
// let lastIndexOf = function (array, value) {
// let output = 0;
// for (let i = array.length - 1; i >= 0; i--) {
// if (array[i] === value) {
// return i;
// }
// output = i;
// } return output;
// }
// console.log(lastIndexOf([0, 1, 4, 1, 2], 1), "=?", 3);
// console.log(lastIndexOf([0, 1, 4, 1, 2], 2), "=?", 4);
// console.log(lastIndexOf([0, 1, 4, 1, 2], 3), "=?", -1);
// console.log(lastIndexOf([5, 5, 5], 5), "=?", 2);
// console.log(lastIndexOf([], 3), "=?", -1);