Flatten a nested array and join the result with commas.
Acceptance criteria
- 1.#out should show 1,2,3,4.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array flat chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “JavaScript flat turns a nested array into a shallower one.” .
1.Flatten one levelexercise · easy · 10 min
Flatten a nested array and join the result with commas.
Passes when: #out should show 1,2,3,4.
2.Flatten two levelsexercise · easy · 10 min · practice plan
Flatten an array nested two deep and show the length.
Passes when: #out should show 4.
3.Flatten completelyexercise · easy · 10 min · practice plan
Use Infinity to flatten however deep the nesting goes.
Passes when: #out should show 1,2,3,4.
4.Split into wordsexercise · easy · 10 min · practice plan
Use flatMap to split each sentence into words and join with commas.
Passes when: #out should show a,b,c,d.
5.Merge groups of namesexercise · medium · 10 min · practice plan
Flatten an array of groups and show the total number of people.
Passes when: #out should show 4.
6.Fix the default depthbug fix · easy · 8 min
One level is not enough for an array nested two deep.
Passes when: #out should show 4.
7.Fix map used instead of flatMapbug fix · easy · 8 min · practice plan
map leaves each result nested inside its own array.
Passes when: #out should show a,b,c,d.
8.Fix the result of flat being ignoredbug fix · easy · 8 min · practice plan
flat returns a new array; it does not change the original.
Passes when: #out should show 1,2,3,4.
9.Fix the fixed depth on unknown nestingbug fix · easy · 8 min · practice plan
The nesting is deeper than the depth given.
Passes when: #out should show 1,2,3,4.
10.Fix concat used on nested groupsbug fix · easy · 8 min · practice plan
The groups need flattening into one list of people.
Passes when: #out should show 4.
Between them these tasks cover flat, flatMap and Nested arrays.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.