Use a rest parameter to add 1, 2, 3 and 4.
Acceptance criteria
- 1.#out should show 10.
JavaScript Tutorial · JavaScript Functions
These 10 tasks go with the JavaScript Rest Parameters 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 rest parameters collect any number of arguments into a single array.” .
1.Add up any number of valuesexercise · easy · 10 min
Use a rest parameter to add 1, 2, 3 and 4.
Passes when: #out should show 10.
2.Count the extrasexercise · easy · 10 min · practice plan
Count how many scores were passed after the title.
Passes when: #out should show 3.
3.An empty rest parameterexercise · easy · 10 min · practice plan
Show the length when no extra arguments are given.
Passes when: #out should show 0.
4.Use an array methodexercise · easy · 10 min · practice plan
Find the largest of 5, 12, 8 and 44 using a rest parameter.
Passes when: #out should show 44.
5.Spread into a rest parameterexercise · medium · 10 min · practice plan
Spread an array of three items into a function that collects them, then show the count.
Passes when: #out should show 3.
6.Fix the missing dotsbug fix · easy · 8 min
Without the three dots only the first argument is received.
Passes when: #out should show 10.
7.Fix the rest parameter used firstbug fix · easy · 8 min · practice plan
A rest parameter must be last, so the title is being swallowed.
Passes when: #out should show 3.
8.Fix the undefined lengthbug fix · easy · 8 min · practice plan
Reading length on a plain parameter that was never passed fails.
Passes when: #out should show 0.
9.Fix the array passed wholebug fix · easy · 8 min · practice plan
Math.max needs separate numbers, not one array.
Passes when: #out should show 44.
10.Fix the array passed as one argumentbug fix · easy · 8 min · practice plan
Without spreading, the function receives a single array.
Passes when: #out should show 3.
Between them these tasks cover Rest parameters, Variable arguments and Rest vs spread.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.