Commenting out code to find a bug

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

Commenting out code to find a bug

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You want JavaScript to skip this line while you hunt for a bug: console.log(total); Which version does that?
A // console.log(total);
B console.log(total); //
2. What happens when you comment out a line with //?
A JavaScript skips the line but it stays in your file
B JavaScript deletes the line from your file
3. You commented out a line and the bug disappeared. What does this suggest?
A The bug is probably caused by that line
B The bug is definitely somewhere else
4. Which is the best way to find a bug by commenting out?
A Comment out every line at once, then run the program
B Comment out one line, run the program, then check the result
5. You finished testing. How do you restore the line // let score = 10;?
A Add another // so it reads //// let score = 10;
B Remove the // so it reads let score = 10;

Match the Pairs

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

Left columnRight column
1// console.log(total); Aconsole.log(total);
2/* let bonus = 5; score += bonus; */ BThis line is commented out, so JavaScript skips it and does not print total.
3To test if a line is causing a bug, switch it off by putting // in front of it. CThe block comment is never closed. It needs */ at the end, or the rest of the program is swallowed and an error appears.
4After you finish testing, restore the code by removing the // so the line runs again. DOnly one slash was typed. A comment needs two slashes //, so JavaScript sees a syntax error instead of a skipped line.
5/ console.log(total); EA block comment that switches off several lines at once so none of them run.
6/* let bonus = 5; score += bonus; F// score = score * 2;

Definitions

// console.log(total);

Fill in the Blanks

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

1.
__ console.log(total);
2.
let total = 0; __ total = total - 5; console.log(total);
3.
__ console.log(a); console.log(b); */
4.
/* total = total - 5; __ console.log(total);
5.
let total = 4 * 5; // Line below is switched back on ___________(total);

Spot the Error

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

Question 1
A # console.log(score * 2);
B let score = 10;
C score = score + 5;
D console.log(score);
Question 2
A let total = 0;
B total = total + 5;
C / total = total * 2;
D console.log(total);
Question 3
A */
B console.log("one");
C console.log("two");
D */
E console.log("three");
Question 4
A let total = 0;
B for (let i = 1; i < 4; i++) {
C total += i;
D // }
E console.log(total);
Question 5
A let a = 3;
B console.log(a + b); //
C let b = 4;
D console.log(a * b);

Answer Key

Commenting out code to find a bug
Section A: Quick Questions
Q1
A — // console.log(total);
Q2
A — JavaScript skips the line but it stays in your file
Q3
A — The bug is probably caused by that line
Q4
B — Comment out one line, run the program, then check the result
Q5
B — Remove the // so it reads let score = 10;
Section B: Match the Pairs
Q1
// console.log(total); → This line is commented out, so JavaScript skips it and does not print total.
Q2
/* let bonus = 5; score += bonus; */ → A block comment that switches off several lines at once so none of them run.
Q3
To test if a line is causing a bug, switch it off by putting // in front of it. → // score = score * 2;
Q4
After you finish testing, restore the code by removing the // so the line runs again. → console.log(total);
Q5
/ console.log(total); → Only one slash was typed. A comment needs two slashes //, so JavaScript sees a syntax error instead of a skipped line.
Q6
/* let bonus = 5; score += bonus; → The block comment is never closed. It needs */ at the end, or the rest of the program is swallowed and an error appears.
Section C: Definitions
Q1
// console.log(total); — Putting // in front of a line makes JavaScript skip it, so you can switch code off to find a bug. Remove the // to switch it back on.
Section D: Fill in the Blanks
Q1
// console.log(total);
Q2
let total = 0; // total = total - 5; console.log(total);
Q3
/* console.log(a); console.log(b); */
Q4
/* total = total - 5; */ console.log(total);
Q5
let total = 4 * 5; // Line below is switched back on console.log(total);
Section E: Spot the Error
Q1
A — # is not a comment in JavaScript, so this line is a syntax error. To switch a line off you put // in front of it: // console.log(score * 2);
Q2
C — A single / does not start a comment, so JavaScript tries to read it as division and gives a syntax error. A comment needs two slashes: //
Q3
A — A block comment must start with /* and end with */. Starting with */ is a syntax error, so the first line should be /*
Q4
D — Commenting out the closing brace leaves the loop open and causes a syntax error. Only comment out whole statements you want to test, and keep paired symbols like { } together. The line should be }
Q5
B — The // only switches off the text after it, so console.log(a + b); still runs and causes an error because b is not ready yet. Put // at the start of the line: // console.log(a + b);