Logic errors: runs but gives the wrong answer

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

Logic errors: runs but gives the wrong answer

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program runs with no error message but prints the wrong total. What kind of mistake is this?
A A logic error
B A syntax error
2. Students must be 13 or older to join. Which condition has a logic error?
A if (age > 13)
B if (age >= 13)
3. A pass mark is 50 and a student scores exactly 50. Which line wrongly says they fail?
A if (score > 50) { console.log("Pass"); }
B if (score >= 50) { console.log("Pass"); }
4. You want the area of a rectangle: width times height. Which line has a logic error?
A let area = width * height;
B let area = width + height;
5. This code should check if a password matches: if (password = "cats"). What is the problem?
A It is fine, because = checks if two values are equal
B It uses = (assignment) when it should use === (comparison)

Match the Pairs

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

Left columnRight column
1if (age > 13) { Aif (answer === 7) {
2let area = width + height; Bif (mark >= 50) {
3Using >= so that a mark of exactly 50 counts as a pass fixes a boundary logic error Cprompt() gives back text, so + joins the text and 5 together ("35") instead of adding. The text should be converted with Number() first.
4Use === to compare two values, because a single = assigns a value instead of comparing DRuns with no error but leaves out a person who is exactly 13, which is a logic error if 13 should be allowed
5if (guess = 7) { EA single = assigns 7 to guess, so the condition is always true. There is no error message, but the answer is wrong. It should be ===.
6let total = prompt("Number?") + 5; FRuns with no error but adds instead of multiplying, so the area is wrong. This is a wrong-operator logic error

Definitions

logic error

Fill in the Blanks

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

1.
let score = 50; if (score __ 50) console.log("Pass");
2.
let price = 8; let total = price _ 3; console.log(total);
3.
let pin = 1234; if (pin ___ 1234) console.log("Open");
4.
let age = prompt("Age?"); let next = ______(age) + 1; console.log(next);
5.
let a = 80; let b = 90; let avg = (a + b) _ 2;

Spot the Error

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

Question 1
A let score = 50;
B if (score > 50) {
C console.log("Pass");
D }
Question 2
A let price = 20;
B let qty = 3;
C let total = price + qty;
D console.log(total);
Question 3
A let colour = "red";
B if (colour = "red") {
C console.log("Stop");
D }
Question 4
A let a = 5;
B let b = 7;
C let c = 9;
D let sum = "" + a + b + c;
E console.log(sum);
Question 5
A let avg = 80 + 90 / 2;
B console.log("Average:", avg);
C console.log(avg >= 80);

Answer Key

Logic errors: runs but gives the wrong answer
Section A: Quick Questions
Q1
A — A logic error
Q2
A — if (age > 13)
Q3
A — if (score > 50) { console.log("Pass"); }
Q4
B — let area = width + height;
Q5
B — It uses = (assignment) when it should use === (comparison)
Section B: Match the Pairs
Q1
if (age > 13) { → Runs with no error but leaves out a person who is exactly 13, which is a logic error if 13 should be allowed
Q2
let area = width + height; → Runs with no error but adds instead of multiplying, so the area is wrong. This is a wrong-operator logic error
Q3
Using >= so that a mark of exactly 50 counts as a pass fixes a boundary logic error → if (mark >= 50) {
Q4
Use === to compare two values, because a single = assigns a value instead of comparing → if (answer === 7) {
Q5
if (guess = 7) { → A single = assigns 7 to guess, so the condition is always true. There is no error message, but the answer is wrong. It should be ===.
Q6
let total = prompt("Number?") + 5; → prompt() gives back text, so + joins the text and 5 together ("35") instead of adding. The text should be converted with Number() first.
Section C: Definitions
Q1
logic error — A mistake that lets the program run with no error message but gives a wrong result, such as using > instead of >=, the wrong operator, or steps in the wrong order.
Section D: Fill in the Blanks
Q1
let score = 50; if (score >= 50) console.log("Pass");
Q2
let price = 8; let total = price * 3; console.log(total);
Q3
let pin = 1234; if (pin === 1234) console.log("Open");
Q4
let age = prompt("Age?"); let next = Number(age) + 1; console.log(next);
Q5
let a = 80; let b = 90; let avg = (a + b) / 2;
Section E: Spot the Error
Q1
B — The program runs, but > leaves out a score of exactly 50, so a student who should pass is told nothing. It should be >= to include 50.
Q2
C — Adding gives 23 instead of the cost of 3 items. The total needs multiplication: price * qty gives 60. There is no error message, just a wrong answer.
Q3
B — A single = assigns a value instead of comparing, so the condition is always true. Use === to compare.
Q4
D — Starting with an empty string makes + join the values as text, so the result is "579" instead of 21. It should be a + b + c.
Q5
A — Without brackets the division happens first, giving 125 instead of 85. Brackets are needed around the addition: (80 + 90) / 2.