How to implement the function lastIndexOfAny() in TypeScript

1 Answer

0 votes
function lastIndexOfAny(str: string, array: string[]) {
	let highestIndex: number = -1;

	for (let i in array) {
 		let index: number = str.lastIndexOf(array[i]);
            
 		if (index > highestIndex) {
        highestIndex = index;
 
        if (index == str.length - 1)
            break;
        }
    }
 
    return highestIndex;
}

const str: string = "typescript programming";
           
const i: number = lastIndexOfAny(str, ['z', 'x', 'i']);
           
console.log(i);
  
  
  
  
  
/*
run:
   
19

*/

 

 



answered Sep 3, 2023 by avibootz
...