Problem overview
The method longWords(words, n) receives an array of strings and a number. It returns a new array with the words that have more than n characters, in their original order.
For words = ["apple", "kiwi", "banana", "fig"] and n = 4, the lengths are 5, 4, 6 and 3. Only "apple" and "banana" are longer than 4, so the result is ["apple", "banana"]. "kiwi" has exactly 4 characters and is left out.
Approach
This is a filter: look at each item once and keep the ones that pass a test. You collect the survivors in a new array and leave words unchanged. Because the loop visits the words from first to last and push adds at the end, the kept words come out in their original order without any extra work.
Step by step
- Create an empty array named
result. Useletto declare it. - Loop over the words with
for...of. It gives you each string directly, with no index to manage. - Test the length. The condition is
word.length > n. Strictly greater matters: with>=, a word of exactlyncharacters would slip in. - Push the word onto
resultwhen the test passes. - Return after the loop. If no word passed,
resultis still an empty array, which is the correct answer.
Tracing the example
Here is what the loop sees for words = ["apple", "kiwi", "banana", "fig"] and n = 4. Each row shows the word, its length, whether length > n holds, and what result contained just before that word was handled.
| word | length | kept? | result before |
|---|---|---|---|
| apple | 5 | true | [] |
| kiwi | 4 | false | ["apple"] |
| banana | 6 | true | ["apple"] |
| fig | 3 | false | ["apple", "banana"] |
After "fig" the loop ends and the function returns ["apple", "banana"]. The array grows only on the rows where the test is true, and "kiwi", which sits exactly on the boundary, is skipped. Notice also that the input array is never modified: the words are copied by reference into a separate array, so the caller's words still has all four entries afterwards.
The solution in JavaScript
class Solution {
longWords(words, n) {
let result = [];
for (let word of words) {
if (word.length > n) {
result.push(word);
}
}
return result;
}
}
Two details are worth knowing. First, length on a string is a property, not a method: write word.length, not word.length(). Second, push changes the array it is called on. Declaring result with let is fine here, and const would work too, since the variable is never reassigned, only its contents change.
The same filter can be written as words.filter(word => word.length > n). The explicit loop shows what filter does underneath.
The number n is a plain parameter, so the same function answers different questions: n = 0 keeps everything, a large n keeps nothing, and n = 4 keeps the words of five or more letters. Because the test is a single comparison, the whole function costs one pass over the array, and each word's length is read in constant time.
Edge cases and common mistakes
- Using
>=instead of>. For["kiwi"]withn = 4the answer must be an empty array. - Returning inside the loop. A
returnin theifwould stop at the first long word and send back one string instead of an array. - Repeated words. They stay.
["go", "went", "gone", "went"]withn = 3gives["went", "gone", "went"]. n = 0. Every word has at least one letter, so every word is kept:["ab", "cd"]gives["ab", "cd"].
Testing
["apple", "kiwi", "banana", "fig"],n = 4gives["apple", "banana"].["hi", "to", "be"],n = 3gives[].["go", "went", "gone", "went"],n = 3gives["went", "gone", "went"].["ab", "cd"],n = 0gives["ab", "cd"].
