Modifying an existing JavaScript program

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

Modifying an existing JavaScript program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You are given a working JavaScript program and asked to change it. What should you do first?
A Delete lines until it looks shorter
B Read the code and predict what it will do
2. Which is the better way to modify a program?
A Change one small part, then run it
B Change five parts, then run it once
3. You change the loop in for (let i = 1; i <= 3; i++) to i <= 5. What should you do next?
A Run it and compare the output with your prediction
B Assume it works and move on without running
4. Before editing a working program, what is a smart way to protect it?
A Keep a copy of the original
B Overwrite it and hope you remember it
5. A program prints 'Hello, Sam!'. You change the message text to 'Welcome, Sam!'. What kind of change is this?
A Adding a brand new loop
B A small change to a message

Match the Pairs

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

Left columnRight column
1let lives = 3; // try 5 instead AKeep a copy of the original line as a comment before you change it
2// let score = 0; let score = 10; BChange one small value, then run the program again to see what is different
3Add a new branch to an existing if statement so the program handles one more case CThe student ran the program without predicting the result first, so there is nothing to compare against
4Change a loop's condition, then run it and compare the output with your prediction Delse if (score >= 50) { console.log("Pass"); }
5// changed the loop limit, the message AND the score all at once let limit = 10; let msg = "Go!"; let score = 100; Efor (let i = 0; i < 5; i++) { console.log(i); }
6let total = 0; total += 5; console.log(total); // I did not predict the output, just ran it FToo many changes were made at once, so if the program breaks you cannot tell which change caused it

Definitions

predict, change, run

Fill in the Blanks

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

1.
// change: repeat 5 times for (let i = 0; i < _; i++) { console.log("Go!");
2.
if (score >= 90) { grade = "A"; } _______ (score >= 80) { grade = "B"; } else { grade = "C"; }
3.
let score = 10; const original = _____; score = 20;
4.
// change: 18 year olds can now enter if (age __ 18) console.log("Enter");
5.
let total = 0; for (let i = 1; i <= 3; i++) total += i; console.log(_____);

Spot the Error

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

Question 1
A let lives = 3;
B lives = lives -- 1;
C console.log(lives);
D // Predict: 2
Question 2
A // Changed from 3 laps to 5
B for (let i = 1; i <= 5; i++) {
C console.log("Lap " + 1);
D }
Question 3
A let temp = 31;
B if (temp > 30 {
C console.log("Hot");
D } else if (temp > 20) {
E console.log("Warm");
F }
Question 4
A let mark = 72;
B let grade = "C";
C if (mark = 80) {
D grade = "B";
E }
F console.log(grade);
Question 5
A let price = 20;
B let original = price;
C price = price + 5;
D console.log(orignal);
E console.log(price);

Answer Key

Modifying an existing JavaScript program
Section A: Quick Questions
Q1
B — Read the code and predict what it will do
Q2
A — Change one small part, then run it
Q3
A — Run it and compare the output with your prediction
Q4
A — Keep a copy of the original
Q5
B — A small change to a message
Section B: Match the Pairs
Q1
let lives = 3; // try 5 instead → Change one small value, then run the program again to see what is different
Q2
// let score = 0; let score = 10; → Keep a copy of the original line as a comment before you change it
Q3
Add a new branch to an existing if statement so the program handles one more case → else if (score >= 50) { console.log("Pass"); }
Q4
Change a loop's condition, then run it and compare the output with your prediction → for (let i = 0; i < 5; i++) { console.log(i); }
Q5
// changed the loop limit, the message AND the score all at once let limit = 10; let msg = "Go!"; let score = 100; → Too many changes were made at once, so if the program breaks you cannot tell which change caused it
Q6
let total = 0; total += 5; console.log(total); // I did not predict the output, just ran it → The student ran the program without predicting the result first, so there is nothing to compare against
Section C: Definitions
Q1
predict, change, run — Read the code and predict what it does, change one small part (a value, condition or message), run it, then compare the result with your prediction.
Section D: Fill in the Blanks
Q1
// change: repeat 5 times for (let i = 0; i < 5; i++) { console.log("Go!");
Q2
if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else { grade = "C"; }
Q3
let score = 10; const original = score; score = 20;
Q4
// change: 18 year olds can now enter if (age >= 18) console.log("Enter");
Q5
let total = 0; for (let i = 1; i <= 3; i++) total += i; console.log(total);
Section E: Spot the Error
Q1
B — The change to the value uses the wrong operator. '--' is not valid between two values, so the program will not run. It should be 'lives = lives - 1;'.
Q2
C — The message uses the number 1 instead of the loop counter i, so every lap prints 'Lap 1'. It should be console.log("Lap " + i);
Q3
B — The closing bracket of the condition is missing, so there is a syntax error. It should be 'if (temp > 30) {'.
Q4
C — A single = assigns a value instead of comparing. The changed condition should use >= to compare: 'if (mark >= 80) {'.
Q5
D — The name of the copy is misspelled, so JavaScript throws a ReferenceError. It should be console.log(original); to print the saved copy.