Using a loop inside a function

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Using a loop inside a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function correctly returns the total of 1 up to n?
A function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; }
B function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } }
2. Where should the accumulator let total = 0; be declared in sumTo(n)?
A Inside the loop, at the top of each pass
B Inside the function, before the loop starts
3. What does sumTo(3) return if the function is written correctly?
A 6
B 3
4. Which statement about the loop in sumTo(n) is true?
A The loop always repeats exactly 10 times, whatever n is
B The loop uses the parameter n to decide how many times to repeat
5. A student wrote return total; as the very first line inside the for loop body. What happens?
A The function ends on the first pass and returns too early
B The loop finishes all its passes, then the function returns

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; } AThe total is declared outside the function, so it keeps its old value between calls and the second call gives a wrong, bigger answer.
2let total = 0; BThe return is inside the loop, so the function stops after the first repeat and returns 1 instead of the full sum.
3The return must come after the loop ends, so the loop can finish adding every number first CA function that loops from 1 up to n, adds each number to a total, then returns the finished total after the loop
4The loop uses the parameter n to decide how many times to repeat DDeclares the accumulator inside the function so it starts at 0 every time the function is called
5function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } } Efor (let i = 1; i <= n; i++) { total += i; } return total;
6let total = 0; function sumTo(n) { for (let i = 1; i <= n; i++) { total += i; } return total; } Ffor (let i = 1; i <= n; i++)

Definitions

function sumTo(n)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
function sumTo(n) { ___ total = 0; for (let i = 1; i <= n; i++) {
2.
} ______ total; }
3.
function sumTo(n) { let total = 0; for (let i = 1; i <= _; i++) {
4.
for (let i = 1; i <= n; i++) { total __ i; }
5.
// 1 + 2 + 3 + 4 gives 10 let result = _____(4); console.log(result);

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A function sumTo(n) {
B let total = 0;
C for (let i=1; i<n; i++)
D total += i;
E return total;
F }
Question 2
A function countEvens() {
B let count = 0;
C for (let i=2; i<=n; i+=2)
D count++;
E return count;
F }
Question 3
A function stars(n) {
B let row = "";
C for (let i=0; i<n; i--)
D row += "*";
E return row;
F }
Question 4
A function factorial(n) {
B let result = 1;
C for (let i=1, i<=n, i++)
D result *= i;
E return result;
F }
Question 5
A function sumSquares(n {
B let total = 0;
C for (let i=1; i<=n; i++)
D total += i * i;
E return total;
F }

Answer Key

Using a loop inside a function
Section A: Quick Questions
Q1
A — function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; }
Q2
B — Inside the function, before the loop starts
Q3
A — 6
Q4
B — The loop uses the parameter n to decide how many times to repeat
Q5
A — The function ends on the first pass and returns too early
Section B: Match the Pairs
Q1
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; } return total; } → A function that loops from 1 up to n, adds each number to a total, then returns the finished total after the loop
Q2
let total = 0; → Declares the accumulator inside the function so it starts at 0 every time the function is called
Q3
The return must come after the loop ends, so the loop can finish adding every number first → for (let i = 1; i <= n; i++) { total += i; } return total;
Q4
The loop uses the parameter n to decide how many times to repeat → for (let i = 1; i <= n; i++)
Q5
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) { total += i; return total; } } → The return is inside the loop, so the function stops after the first repeat and returns 1 instead of the full sum.
Q6
let total = 0; function sumTo(n) { for (let i = 1; i <= n; i++) { total += i; } return total; } → The total is declared outside the function, so it keeps its old value between calls and the second call gives a wrong, bigger answer.
Section C: Definitions
Q1
function sumTo(n) — A function that loops up to its parameter n, adds each number to a total declared inside it, then returns that total after the loop ends so the caller gets the finished answer.
Section D: Fill in the Blanks
Q1
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) {
Q2
} return total; }
Q3
function sumTo(n) { let total = 0; for (let i = 1; i <= n; i++) {
Q4
for (let i = 1; i <= n; i++) { total += i; }
Q5
// 1 + 2 + 3 + 4 gives 10 let result = sumTo(4); console.log(result);
Section E: Spot the Error
Q1
C — The loop uses i < n, so it stops before adding n. sumTo(5) would give 10 instead of 15. It should be i<=n so the last number is included.
Q2
A — The function has no parameter, so n is not defined inside the loop condition. The header should be function countEvens(n) {.
Q3
C — i-- makes the counter go down, so i<n stays true forever and the loop never ends. It should be i++ to count up.
Q4
C — The three parts of a for loop header are separated by semicolons, not commas. It should be for (let i=1; i<=n; i++).
Q5
A — The closing bracket after the parameter is missing. The function header should be function sumSquares(n) {.