How to implement the function indexOfAny() in TypeScript

1 Answer

0 votes
function indexOfAny(str: string, array: any[]) {
    let lowestIndex: number = -1;
 
    for (let i in array) {
        let index: number = str.indexOf(array[i]);
 
        if (index > -1) {
            if (lowestIndex == -1 || index < lowestIndex) {
                lowestIndex = index;
                if (index == 0) {
                    break;
                }
            }
        }
    }
  
    return lowestIndex;
}
 
const str: string = "typescript programming";
          
const i: number = indexOfAny(str, ['u', 'x', 'o']);
          
console.log(i);
 
 
 
 
 
/*
run:
  
13
  
*/

 



answered Sep 2, 2023 by avibootz

Related questions

1 answer 114 views
1 answer 160 views
1 answer 122 views
1 answer 77 views
1 answer 101 views
1 answer 133 views
1 answer 144 views
144 views asked Sep 29, 2023 by avibootz
...