Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created May 24, 2025 16:28
Show Gist options
  • Save tatsuyax25/0e1d0f5b4316d16ba0adf77fe6cd783b to your computer and use it in GitHub Desktop.
Save tatsuyax25/0e1d0f5b4316d16ba0adf77fe6cd783b to your computer and use it in GitHub Desktop.
You are given a 0-indexed array of strings words and a character x. Return an array of indices representing the words that contain the character x. Note that the returned array may be in any order.
/**
* @param {string[]} words
* @param {character} x
* @return {number[]}
*/
var findWordsContaining = function(words, x) {
const indices = []; // Array to store indices of words containing 'x'
// Iterate through each word in the 'words' array
for (let i = 0; i < words.length; i++) {
if (words[i].includes(x)) { // Check if the word contains the target character
indices.push(i); // Store the index if condition is met
}
}
return indices; // Return the array of indices
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment