-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathlastIndexOf.js
29 lines (26 loc) · 948 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
/**
* Given an array and a value, returns the index of the last occurence of
* the value. If the value is not found, returns -1.
*
* The array doesn't need to contain a single type of data.
*
* @example
* lastIndexOf([10, 20, 30, 20], 20); // => 3
* lastIndexOf([10, 20, 30, 20], 17); // => -1
* lastIndexOf(['giraffe', giraffe', 'banana'], 'giraffe'); // => 1
* lastIndexOf(['giraffe', giraffe', 'banana'], 'banana'); // => 2
*
* @param {object[]} haystack - An array
* @param {object} needle - The value to search for
* @returns {boolean} The index of the last occurrence of the value in the
* array, or -1 if it's not found.
*/
function lastIndexOf(haystack, needle) {
// This is your job. :)
}
if (require.main === module) {
console.log('Running sanity checks for lastIndexOf:');
// Add your own sanity checks here.
// How else will you be sure your code does what you think it does?
}
module.exports = lastIndexOf;