How to implement the function indexOfAny() in JavaScript

1 Answer

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

 



answered Sep 2, 2023 by avibootz
edited Sep 2, 2023 by avibootz

Related questions

1 answer 167 views
1 answer 105 views
1 answer 113 views
1 answer 132 views
132 views asked Sep 29, 2023 by avibootz
1 answer 121 views
...