How to get the indexes of words from an array of strings that start with a specific letter in JavaScript

1 Answer

0 votes
function getIndexesOfWordsStartingWith(array, letter) {
    const indexes = [];
    
    array.forEach((word, index) => {
        if (word.toLowerCase().startsWith(letter.toLowerCase())) {
            indexes.push(index);
        }
    });
    
    return indexes;
}

const stringArray = ["zero", "one", "two", "three", "four", "five", 
                     "six", "seven", "eight", "nine", "ten"];
const specificLetter = 't';

const indexes = getIndexesOfWordsStartingWith(stringArray, specificLetter);

console.log(indexes); 



/*
Run:
 
[ 2, 3, 10 ]
 
*/

 



answered Mar 14, 2025 by avibootz
...