How to implement the function lastIndexOfAny() in Node.js

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 = "node.js programming";
           
const i = lastIndexOfAny(str, ['y', 'x', 'o']);
           
console.log(i);
  
  
  
  
  
/*
run:
   
10

*/

 

 



answered Sep 3, 2023 by avibootz

Related questions

1 answer 133 views
1 answer 132 views
1 answer 122 views
1 answer 79 views
1 answer 114 views
1 answer 79 views
1 answer 138 views
...