locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
18 lines (17 loc) • 707 B
JavaScript
export function lastindexof(needle, haystack, startIndex) {
// discuss at: https://locutus.io/powershell/lastindexof/
// parity verified: PowerShell 7.4
// original by: Kevin van Zonneveld (https://kvz.io)
// example 1: lastindexof('o', 'Hello World')
// returns 1: 7
// example 2: lastindexof('x', 'Hello World')
// returns 2: -1
// example 3: lastindexof('o', 'Hello World', 6)
// returns 3: 4
const source = String(haystack);
const search = String(needle);
if (startIndex === undefined) {
return source.lastIndexOf(search);
}
return source.lastIndexOf(search, Math.trunc(Number(startIndex)));
}