Exam JavaScript-Developer-I Topic 2 Question 45 Discussion

Actual exam question for Salesforce's JavaScript-Developer-I exam
Question #: 45
Topic #: 2
Refer to the following code block:
01 let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
02 let output = 0;
03
04 for (let num of array) {
05 if (output > 10) {
06 break;
07 }
08 if (num % 2 == 0) {
09 continue;
10 }
11 output += num;
12 }
What is the value of output after the code executes?

Suggested Answer: A Vote an answer

This code uses:
* A for...of loop to iterate over values in array.
* break to exit the loop entirely when output > 10.
* continue to skip even numbers.
* It sums only certain numbers into output.
Let's walk through the loop step by step.
Initial values:
* array = [1,2,3,4,5,6,7,8,9,10,11]
* output = 0
Loop: for (let num of array) { ... }
* First iteration: num = 1
* Line 05: if (output > 10) # 0 > 10 is false # no break.
* Line 08: if (num % 2 == 0) # 1 % 2 == 1, not 0, so false # no continue.
* Line 11: output += num # output = 0 + 1 = 1.
* Second iteration: num = 2
* output > 10 # 1 > 10 is false # no break.
* num % 2 == 0 # 2 % 2 == 0, so true # continue.
* Because of continue, line 11 is skipped.
* output remains 1.
* Third iteration: num = 3
* output > 10 # 1 > 10 is false.
* num % 2 == 0 # 3 % 2 == 1, false # no continue.
* output += num # output = 1 + 3 = 4.
* Fourth iteration: num = 4
* output > 10 # 4 > 10 is false.
* num % 2 == 0 # 4 % 2 == 0, true # continue.
* Skip sum; output remains 4.
* Fifth iteration: num = 5
* output > 10 # 4 > 10 is false.
* num % 2 == 0 # 5 % 2 == 1, false.
* output += num # output = 4 + 5 = 9.
* Sixth iteration: num = 6
* output > 10 # 9 > 10 is false.
* num % 2 == 0 # 6 % 2 == 0, true # continue.
* output remains 9.
* Seventh iteration: num = 7
* output > 10 # 9 > 10 is false.
* num % 2 == 0 # 7 % 2 == 1, false.
* output += num # output = 9 + 7 = 16.
* Eighth iteration would be num = 8, but:
At the top of the loop body, line 05 is checked again:
* if (output > 10) # 16 > 10 is true, so break; is executed.
When break runs:
* The loop terminates immediately.
* No further iterations (for num = 8, 9, 10, 11) are executed.
* Therefore, output stays at 16.
Final value of output after the loop ends is 16.
This matches option A.
Why other options do not match:
* B. 25: Would require adding more odd numbers (e.g., 9, 11) after 7, but the loop stops early due to output > 10.
* C. 11: Would be smaller; the actual sum of 1 + 3 + 5 + 7 until break is 16.
* D. 36: Would require summing many more values (e.g., most or all odd numbers up to 11), but again, the break condition stops the loop once output exceeds 10.
So:
The answer: A
JavaScript knowledge / Study Guide references (concept names only, no links):
* for...of loop over arrays
* break statement in loops (terminating a loop early)
* continue statement in loops (skipping to the next iteration)
* Modulo operator % to test even and odd numbers
* Step-by-step execution and control flow in loops

by Amos at Jul 01, 2026, 12:56 PM

Comments

Chosen Answer:
This is a voting comment (?) , you can switch to a simple comment.
Switch to a voting comment New
Nick name: Submit Cancel
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

0
0
0
10