How would you write the rules of a password checker, a parking fee calculator or a quiz scorer so that every case is handled and tested?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
Each rule gets a named boolean; the decision combines them. (The /[0-9]/ part is a regular expression that looks for a digit; you will meet them later.)
Learning objective: Plan a decision-making program from requirements, combine comparisons, logical operators and conditionals, and test it with boundary values.
Decision programs follow a plan: list the rules, give each rule a named boolean, combine them in an if chain, and print the result.
Write the rules in plain language first: "A password is strong when it has at least 8 characters AND a digit." Each "and"/"or" becomes && or ||.
Test boundary values: if the rule is "8 or more", test 7, 8 and 9. Most bugs hide at the edges.
Handle invalid input before the main rules, and give every message a clear reason: "Too short" helps more than "Invalid".
Separate decision logic from input and output: a pure function (Lesson 21) that takes values and returns a decision is easy to test with many cases.
Decision tables enumerate every combination of conditions and the expected outcome. They expose missing cases and are a direct source of test cases.
Equivalence partitioning groups inputs that behave the same; boundary value analysis tests the edges of each group. Both keep test suites small but meaningful.
Prefer data-driven rules (thresholds in constants or configuration) over magic numbers spread through conditions, so rules can change without rewriting logic.
05Definitions
Requirement
A statement of what the program must do.
Boundary value
An input at the edge of a rule, such as exactly 8 characters for an "8 or more" rule.
Decision table
A table of condition combinations and the expected result for each.
Magic number
An unexplained number in code; replace it with a named constant.
Edge case
An unusual input, such as zero, empty text or the largest allowed value.
Turn a requirements list into a tested decision program.
// Requirements
// - distance in km, express true/false
// - distance 0 or less → "Invalid distance"
// - up to 10 km → 1 day, up to 100 km → 2 days, beyond → 4 days
// - express removes one day, but never below 1 day
const distance = 80;
const express = true;
09Prediction
Commit to an answer first, then run the real code to compare.
The Tip Calculator's validation stage is a decision program: bill above 0 and at most 100,000, tip 0–100, people 1–50. Each rule is a named check, and the tests probe every boundary.
const people = 51;
console.log(people >= 1 && people <= 50 ? "ok" : "Enter 1 to 50 people");
12Knowledge check
1. Which inputs best test the rule "at least 8 characters"?
1, 100
7, 8, 9
8 only
0
2. Why replace if (fee > 15) with if (fee > DAILY_MAX)?
It runs faster
The rule is named and can change in one place
It is required by JavaScript
It avoids errors
3. What does this print?
const p = 75;
console.log(p >= 90 ? "Excellent" : p >= 70 ? "Pass" : "Fail");
Pass
Fail
Excellent
Nothing
4. What happens when total is 0?
const correct = 0, total = 0;
console.log(correct / total * 100);
It prints 0
It divides by zero and prints NaN or Infinity
It throws an error
It prints nothing
5. What is a decision table for?
Storing data
Listing every combination of conditions and the expected result
Drawing charts
Sorting values
6. Which feedback is more helpful?
"Invalid password"
"Add a digit and a capital letter"
Both are equally helpful
No feedback
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the boundary: Prints Free shipping; Uses >= 50
Library e-book loan: Prints Pay fines first; Named booleans are correct
Delivery time estimator: Prints Delivery in 1 day; days is 1; Applies the minimum of 1 day
ex-015-01: output matches the documented expected output
ex-015-02: output matches the documented expected output
ex-015-03: output matches the documented expected output
ex-015-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-03-decisions-and-logic/lesson-015-building-decision-making-applications/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Start from plain-language rules; give each rule a named boolean.
Validate input first and give specific feedback.
Use named constants instead of magic numbers.
Test boundary values just below, at and above each limit.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationBoundary testing
Context: I am learning to test decision programs.
Task: Explain boundary values and equivalence classes with a parking-fee example.
Constraints: Beginner level.
Expected behaviour: I can choose test inputs for any rule.
Testing: Give me a rule and ask me for test inputs.
PlanningRules to plan
Context: I want to write a decision program for a cinema discount.
Task: Help me write the rules in plain language and a decision table before any code.
Constraints: No code until I approve the table.
Expected behaviour: Every combination has an outcome.
Testing: Ask me which outcomes surprised me.
DebuggingOff by one
Context: My eligibility check fails for exactly the minimum age.
Task: Help me find the comparison error.
Constraints: Questions only.
Expected behaviour: I fix it myself.
Testing: Suggest the three boundary tests.
Exercise generationDecision projects
Context: I finished the delivery estimator.
Task: Create two small decision programs with requirements lists.
Constraints: Exact expected output for three inputs each.
Expected behaviour: Each has a validation rule.
Testing: Include expected outputs.
Code reviewMagic numbers
Context: My decision code has numbers like 30, 3 and 15 in several places.
Task: Show me which to turn into named constants.
Constraints: Keep behaviour identical.
Expected behaviour: No unexplained numbers remain.
Testing: List the constants and their meaning.