Problem overview
The Unix head command prints the beginning of its input, 10 lines by default. This first version builds that default as head(lines). The method receives an array of strings and returns the first 10 of them joined by newline characters. With fewer than 10 lines it returns all of them, and with no lines it returns an empty string.
For the 13 lines "a" to "m", the result is "a\nb\nc\nd\ne\nf\ng\nh\ni\nj", which prints as the letters a to j, one per row.
Approach: slice, then join
The task has two parts. The first is taking at most 10 items, and slice(start, end) does it. It returns a new array with the items from index start up to, but not including, index end. So lines.slice(0, 10) holds the items at indexes 0 to 9, which are the first 10.
The second part is turning those items into text, and join("\n") does that, as in a cat tool: newlines go between lines, not after the last one.
The "fewer than 10" rules need no code of their own. slice stops quietly at the end of the array when the end index is too big. It doesn't throw and it doesn't pad. Three lines give three items, and an empty array gives an empty array, which joins to "".
The same result could come from a loop that copies lines one at a time until it has 10 or runs out, using Math.min(10, lines.length) as the bound. Both work, but the loop needs the bound to be right, while slice carries it inside the method. Because slice returns a copy, the caller's array is also left untouched.
Step by step
The lesson has one step. Return lines.slice(0, 10).join("\n"): slice first to keep the first 10 items, then join the result with newlines.
The solution in JavaScript
class Solution {
head(lines) {
return lines.slice(0, 10).join("\n");
}
}
Language details
slicedoesn't change the array it is called on. Afterheadruns on the 13 letters, the array still has 13 items. (splice, a similarly named method, does modify its array.)- The end index is exclusive, so
slice(0, 10)returns 10 items, not 11. - An end index past the array's length is allowed:
alpha.slice(0, 20).lengthis 13 for the 13-letter array.
Using the method
The method lives in a class, so the code that tests it creates an instance first and then calls it. After const s = new Solution();, the call s.head(["a", "b", "c"]) returns "a\nb\nc", because three lines are fewer than 10. Try it in the editor above and print the result with console.log, which shows the newlines as separate rows.
Testing
The method returns these results for inputs of different sizes:
13 lines -> "a\nb\nc\nd\ne\nf\ng\nh\ni\nj"
exactly 10 lines -> "a\nb\nc\nd\ne\nf\ng\nh\ni\nj"
3 lines -> "a\nb\nc"
0 lines -> ""
Edge cases and common mistakes
- Off by one.
slice(0, 9)returns 9 items. The end index is the count when the start is 0. - Checking the length first. An
if (lines.length < 10)branch is unnecessary, becauseslicealready copes. - Forgetting
join. Returning the sliced array gives an array, not the string the method promises. - The number 10 is fixed here. The next version builds on this one.
