Problem overview
The method longWords receives an array of strings, words, and a number n. It returns a new array with the words that have more than n characters, in their original order.
With words = ["apple", "kiwi", "banana", "fig"] and n = 4, the result is ["apple", "banana"]. "apple" has 5 characters and "banana" has 6. "kiwi" has exactly 4, which is not more than 4, so it is left out. When no word is long enough, the result is the empty array [].
Approach
This is a filter: look at each word once, keep it if it passes a test, skip it otherwise. The kept words go into a new array, and words itself is never changed. Because the loop meets the words from first to last and each kept word is added at the end, the order of words carries over to the result without any extra work.
The test is word.length > n. The comparison is strict: a word with exactly n characters fails it. Writing >= would keep "kiwi" in the first example.
Step by step
- Create the result array. Declare
resultas an emptystring[]. It starts empty because nothing has passed the test yet. - Loop over the words. A
for...ofloop withconst wordgives you one word per round.constis fine because each round gets a fresh variable. - Test the length. Inside the loop, an
ifcomparesword.lengthwithn. - Keep the word. Inside the
if,pushadds the word at the end ofresult. - Return after the loop. Returning inside the loop would end the method after the first word.
The solution in TypeScript
class Solution {
longWords(words: string[], n: number): string[] {
let result: string[] = [];
for (const word of words) {
if (word.length > n) {
result.push(word);
}
}
return result;
}
}
A few details:
lengthis a property. Writeword.lengthwith no parentheses. It counts UTF-16 code units, which for the lowercase English letters of this lesson is the same as the number of characters.pushchanges the array in place. It adds the element toresultand returns the new length, which this code ignores.- The annotations are for the compiler only.
string[]andnumberare removed when TypeScript compiles to JavaScript, so nothing checks them while the code runs. letversusconst. The code never reassignsresult, only changes its contents, soconstwould work too. Both pass the lesson's tests.
The same filter can be written with the array method words.filter(word => word.length > n). The loop shows each step explicitly, which is the point of this lesson.
Edge cases and common mistakes
- Using
>=. A word of exactlyncharacters would be kept. For["kiwi", "fig"]withn = 4, the answer must be[]. - Nothing passes. For
["hi", "to", "be"]withn = 3, the loop never pushes and the method returns[]. n = 0. Every word has at least one letter, so every word is kept:["kiwi", "fig"]comes back unchanged.- Repeated words. Duplicates are kept, each as often as it appears.
["go", "went", "gone", "went"]withn = 3gives["went", "gone", "went"]. - Returning inside the loop. The method would stop at the first word, passed or not.
Testing
["apple", "kiwi", "banana", "fig"]withn = 4gives["apple","banana"].["hi", "to", "be"]withn = 3gives[].["go", "went", "gone", "went"]withn = 3gives["went","gone","went"].["kiwi", "fig"]withn = 0gives["kiwi","fig"].
