Problem overview
The method wordLengths receives an array of strings, words, and returns an array of numbers: the length of each word, in the same order.
For ["I", "love", "pancakes"] the answer is [1, 4, 8]. One word gives one length, so ["code"] gives [4]. Words can contain uppercase letters, and they count like any other: ["Hello", "WORLD"] gives [5, 5].
Approach
Each word produces exactly one number, so the output has as many items as the input. Visit the words from first to last, compute word.length for each, and add it to the end of a new array. Adding at the end is what keeps the order.
Here is the loop on ["I", "love", "pancakes"]. Round one pushes 1, round two pushes 4, round three pushes 8, and lengths grows from [] to [1], [1, 4] and finally [1, 4, 8]. The array is built up one item per word, which is the basic shape of most loops that produce a list: create an empty collection, add to it inside the loop, return it afterwards.
This shape also differs from a filter such as keeping only the long words. A filter pushes inside an if and can end with fewer items than it started with. Here there is no if, so every word contributes, and the result is always exactly as long as words.
Step by step
- Start with an empty array of numbers.
lengths, typednumber[], collects the results. - Loop over the words with
for (const word of words). The loop runs once per word. - Push the length.
word.lengthgoes ontolengths. - Return after the loop. Put
return lengthsbelow the closing brace of the loop. Inside it, only the first length would come back.
The solution in TypeScript
class Solution {
wordLengths(words: string[]): number[] {
let lengths: number[] = [];
for (const word of words) {
lengths.push(word.length);
}
return lengths;
}
}
Details worth knowing:
- Two different arrays. The parameter is a
string[]and the result is anumber[]. Their types differ, but they have the same length. lengthhas no parentheses. It is a property of strings and arrays. Writingword.length()is a type error, and if the JavaScript ran anyway it would fail, because a number can't be called.- Types are erased. After compilation the annotations are gone;
pushandlengthare plain JavaScript.
The one-line version is words.map(word => word.length). The loop does the same work and makes each step visible.
Strings and arrays both have length, so the same property answers two different questions in this lesson. On words it counts the words; on word it counts the letters. Knowing which variable you are asking keeps the two apart.
Edge cases and common mistakes
- Returning inside the loop. The result would hold one number at most.
- Counting words instead of letters.
words.lengthis how many words there are. The length of one word isword.length. - Case. "WORLD" has 5 letters just as "world" does. Nothing needs converting.
- Order. Pushing in loop order keeps it. Sorting the result would break the match between lengths and words.
Testing
["I", "love", "pancakes"]gives[1,4,8].["banana", "kiwi", "fig"]gives[6,4,3].["code"]gives[4].["Hello", "WORLD"]gives[5,5].
