Add every number in the array together and show the total.
Acceptance criteria
- 1.#out should show 100.
JavaScript Tutorial · JavaScript Loops
These 10 tasks go with the JavaScript for of Loop 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: “The JavaScript for of loop is used to loop through the values of an array, a string, and other collections.” .
1.Add up an arrayexercise · easy · 10 min
Add every number in the array together and show the total.
Passes when: #out should show 100.
2.Join the valuesexercise · easy · 10 min · practice plan
Join the fruit names together with a comma.
Passes when: #out should show Apple,Banana,Mango.
3.Count the lettersexercise · easy · 10 min · practice plan
Count how many characters are in the text, ignoring spaces.
Passes when: #out should show 6.
4.Use the index with entriesexercise · easy · 10 min · practice plan
Show each fruit with its index, joined by commas.
Passes when: #out should show 0:Apple,1:Banana.
5.Find the largest numberexercise · medium · 10 min · practice plan
Find the largest number in the array and show it.
Passes when: #out should show 44.
6.Fix for in used instead of for ofbug fix · easy · 8 min
for in gives the index, so the totals are wrong.
Passes when: #out should show 100.
7.Fix the index treated as a valuebug fix · easy · 8 min · practice plan
This joins the indexes instead of the fruit names.
Passes when: #out should show the fruit names.
8.Fix the missing entries callbug fix · easy · 8 min · practice plan
Without entries there is no index to unpack.
Passes when: #out should show the index before the name.
9.Fix the comparison that never updatesbug fix · easy · 8 min · practice plan
The comparison is the wrong way round, so the smallest wins.
Passes when: #out should show 44.
10.Fix the break that should be continuebug fix · easy · 8 min · practice plan
The loop stops at the first space instead of skipping it.
Passes when: #out should show 4.
Between them these tasks cover for of, Iterating values and entries and destructuring.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.