How to implement the function lastIndexOfAny() in JavaScript

1 Answer

0 votes
function lastIndexOfAny(string, array) {
	let highestIndex = -1;

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

const str = "javascript programming";
           
const i = lastIndexOfAny(str, ['y', 'x', 'a']);
           
console.log(i);
  
  
  
  
  
/*
run:
   
16

*/

 

 



answered Sep 3, 2023 by avibootz

Related questions

...