ProgramGuru

Test a School Rule at Its Boundaries

Test a School Rule at Its Boundaries

A useful test checks expected behavior, especially where a decision changes. A pass rule should accept 50 and reject 49; testing only a typical high mark can miss an off-by-one error.

Before this topic, review Debug a Calculation if its terms are unfamiliar.

What you’ll learn

  • Explain test a school rule at its boundaries through the school example.
  • Trace the shown operations and distinguish their result from displayed output.
  • Make the practice change and explain its consequence: The replacement is another valid expectation, so all three cases still pass.

Work through the school example

This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.

JavaScript · school.js · complete example

function passed(mark) {
  return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [80, true]]) {
  if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");

Expected output

3 checks passed
Run in JavaScript compiler →

Recognize the important boundary

Tests do not prove every possible behavior. Keep external-input validation separate from internal development checks, and test error paths as well as success.

Try a small variation

JavaScript · original source for practice

function passed(mark) {
  return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [80, true]]) {
  if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");

Your task: Replace [80,true] with [40,false]. Predict the result before running the edited program.

The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.

Worked answer and reasoning

The replacement is another valid expectation, so all three cases still pass.

JavaScript · complete worked variation

function passed(mark) {
  return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [40, false]]) {
  if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");

Expected output

3 checks passed
Run in JavaScript compiler →

Predict the original program

JavaScript · original program for this question

function passed(mark) {
  return mark >= 50;
}
for (const [mark, expected] of [[49, false], [50, true], [80, true]]) {
  if (passed(mark) !== expected) throw new Error("Failed: " + mark);
}
console.log("3 checks passed");
What does the original JavaScript program display, in order?
Choose one answer. You can try again.

Answer and explanation

3 checks passed

A useful test checks expected behavior, especially where a decision changes. A pass rule should accept 50 and reject 49; testing only a typical high mark can miss an off-by-one error. Tests do not prove every possible behavior. Keep external-input validation separate from internal development checks, and test error paths as well as success.

Check the important distinction

Which pair directly checks the pass threshold at 50?
Choose one answer. You can try again.

Answer and explanation

49 and 50

The adjacent values straddle the threshold and reveal >= versus > mistakes.

Keep the useful idea

A useful test checks expected behavior, especially where a decision changes. A pass rule should accept 50 and reject 49; testing only a typical high mark can miss an off-by-one error.

Check your variation against this explanation: The replacement is another valid expectation, so all three cases still pass.

Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Use Browser Facilities Deliberately →