Without break the loop keeps going and reports the last match.
Acceptance criteria
- 1.#out should show 4, not 8.
JavaScript Tutorial · JavaScript Loops
These 10 tasks go with the JavaScript break Statement 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 break statement is used to stop a loop before it normally ends.” .
1.Stop at a matchexercise · easy · 10 min
Scan 1..10 and stop at the first number divisible by 4. Show it.
Passes when: #out should show 4.
2.Count until breakexercise · easy · 10 min · practice plan
Count how many iterations run before breaking at 3.
Passes when: #out should show 3.
3.Skip with continueexercise · easy · 10 min · practice plan
Sum 1..5 but skip the number 3.
Passes when: #out should show 12.
4.Find an item in a listexercise · easy · 10 min · practice plan
Find "Grace" in the list and show her position (1-based).
Passes when: #out should show 3.
5.Break out of a search earlyexercise · medium · 10 min · practice plan
Stop scanning as soon as a negative number appears and report it.
Passes when: #out should show -2.
6.Fix the missing breakbug fix · easy · 8 min
Without break the loop keeps going and reports the last match.
Passes when: #out should show 4, not 8.
7.Fix break used instead of continuebug fix · easy · 8 min · practice plan
break stops the whole loop when only one value should be skipped.
Passes when: #out should show 12.
8.Fix the break placed too earlybug fix · easy · 8 min · practice plan
Breaking before the work happens means nothing is counted.
Passes when: #out should show 3.
9.Fix the zero-based positionbug fix · easy · 8 min · practice plan
The reported position is off by one.
Passes when: #out should show 3.
10.Fix the wrong comparison in the searchbug fix · easy · 8 min · practice plan
The test looks for the wrong condition and never breaks.
Passes when: #out should show -2.
Between them these tasks cover break, continue and Early exit.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.