Use forEach to build a dash-separated string of the values.
Acceptance criteria
- 1.#out should show a-b-c-.
JavaScript Tutorial · JavaScript Arrays
These 10 tasks go with the JavaScript Array forEach 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 forEach runs a function once for every element in an array.” .
1.Build a string with forEachexercise · easy · 10 min
Use forEach to build a dash-separated string of the values.
Passes when: #out should show a-b-c-.
2.Add up with forEachexercise · easy · 10 min · practice plan
Use forEach to total a list of numbers.
Passes when: #out should show 60.
3.Use the indexexercise · easy · 10 min · practice plan
Number each name starting at 1, separated by spaces.
Passes when: #out should show 1.Ada 2.Grace.
4.Count matching valuesexercise · easy · 10 min · practice plan
Use forEach to count how many numbers are above 10.
Passes when: #out should show 2.
5.Show forEach returns nothingexercise · medium · 10 min · practice plan
Store the result of forEach and show its type.
Passes when: #out should show undefined.
6.Fix the result of forEach being usedbug fix · easy · 8 min
forEach returns nothing, so the doubled values were lost.
Passes when: #out should show 2,4,6.
7.Fix the total declared insidebug fix · easy · 8 min · practice plan
Declaring the total inside the callback resets it every time.
Passes when: #out should show 60.
8.Fix the arguments in the wrong orderbug fix · easy · 8 min · practice plan
The value comes first and the index second, so the labels came out backwards.
Passes when: #out should show 0:a 1:b.
9.Fix the numbering starting at zerobug fix · easy · 8 min · practice plan
The list should be numbered from 1.
Passes when: #out should show 1.Ada 2.Grace.
10.Fix the early exit that never happensbug fix · easy · 8 min · practice plan
return inside forEach only skips one turn, so the count keeps rising.
Passes when: #out should show stopped at 2.
Between them these tasks cover forEach, Callbacks and Index argument.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.