You can find all the words that contain different letters using the following steps in JavaScript:
for
loop to iterate through each word in the array.split()
method.for
loop to iterate through each letter in the word.push()
method.Here's the JavaScript code for the above logic:
index.tsx587 chars25 lines
In the above code, we define an array of words, words
, and then define a function, findWordsWithDiffLetters
, that takes an array as its argument and returns an array of words that contain different letters.
We use two nested for
loops to iterate through the array of words and each word's individual letters. We split each word into an array of its individual letters using the split()
method, and then create an empty array, uniqueLetters
, to track the unique letters in the word. We iterate through each letter in the word using another for
loop, and check if the current letter is already in the array of unique letters. If it is not, we add it to the array using the push()
method.
After looping through all the letters in the word, we check if the length of the array of unique letters is equal to the length of the array of all letters in the word. If so, all the letters in the word are unique, so we add the word to the array of words that contain different letters.
Finally, we return the array of words that contain different letters.
gistlibby LogSnag