Back to questions
JuniorTheoryCommon
What does the program print?
let s = 0;
for (let i = 1; i < 5; i++) {
s += i;
}
console.log(s);10
- Expecting
15: the conditioni < 5is false onceireaches5, so the body runs for1,2,3and4only, and the sum is10. - Expecting a line per step:
console.log(s)stands after the loop's closing brace, so it runs once, when the loop has finished.
- What would you change in the loop header to include
5in the sum? - What does the program print if
s += iis replaced withs = i?