JavaScript AcademyProfessional Engineering
Level 1 · FoundationsModule 3: Decisions and LogicLesson 15 of 150About 50 minutes

Lesson 15: Building decision-making applications

01Opening question

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.)

const password = "sunny42";
const longEnough = password.length >= 8;
const hasDigit = /[0-9]/.test(password);
if (longEnough && hasDigit) {
  console.log("Strong enough");
} else {
  console.log("Too weak");
}

03Objective and prerequisites

Learning objective: Plan a decision-making program from requirements, combine comparisons, logical operators and conditionals, and test it with boundary values.

You need: Lesson 13: If, else if and else, Lesson 14: Switch statements and conditional expressions

Skills practised: Logic, Problem solving.

04Explanation

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".

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.

Open the full glossary

06Visual learning object

Conditional decision tree. Trace the decision path for different inputs and compare it with the rules.

Loading the interactive object…

Open the full Conditional decision tree page with its guided experiment.

07Examples

ex-015-01Parking fee rules Browser-executable JavaScript

Named constants keep the rules readable and easy to change.

const FREE_MINUTES = 30;
const HOURLY_RATE = 3;
const DAILY_MAX = 15;
const minutes = 200;
let fee;
if (minutes <= FREE_MINUTES) {
  fee = 0;
} else {
  fee = Math.ceil(minutes / 60) * HOURLY_RATE;
  if (fee > DAILY_MAX) fee = DAILY_MAX;
}
console.log(`${minutes} minutes: $${fee}`);
Sample input
None
Expected output
200 minutes: $12
Common mistake
Hard-coding 3 and 15 in several places.
Try this
Try 25, 61 and 400 minutes.
Source
examples/ex-015-01-parking-fee.js

ex-015-02Testing the boundaries Browser-executable JavaScript

Checking just below, at and above the limit catches off-by-one mistakes.

const MIN_AGE = 18;
const ages = [17, 18, 19];
console.log(ages[0], ages[0] >= MIN_AGE);
console.log(ages[1], ages[1] >= MIN_AGE);
console.log(ages[2], ages[2] >= MIN_AGE);
Sample input
None
Expected output
17 false 18 true 19 true
Common mistake
Using > when the rule says "at least".
Try this
Change >= to > and see which boundary breaks.
Source
examples/ex-015-02-boundaries.js

ex-015-03Quiz result with reasons Browser-executable JavaScript

Each outcome prints a specific reason.

const correct = 7;
const total = 10;
const percent = correct / total * 100;
let result;
if (total === 0) {
  result = "No questions answered";
} else if (percent >= 90) {
  result = "Excellent";
} else if (percent >= 70) {
  result = "Pass";
} else {
  result = `Not yet: ${70 - percent} points below the pass mark`;
}
console.log(`${percent}%: ${result}`);
Sample input
None
Expected output
70%: Pass
Common mistake
Dividing by total before checking it is not zero.
Try this
Try correct = 5.
Source
examples/ex-015-03-quiz-result.js

ex-015-04Specific feedback Browser-executable JavaScript

Checking rules separately lets the program say exactly what is missing.

const password = "sunshine";
const longEnough = password.length >= 8;
const hasDigit = /[0-9]/.test(password);
const hasUpper = password !== password.toLowerCase();
if (!longEnough) console.log("Add more characters");
if (!hasDigit) console.log("Add a digit");
if (!hasUpper) console.log("Add a capital letter");
if (longEnough && hasDigit && hasUpper) console.log("Strong password");
Sample input
None
Expected output
Add a digit Add a capital letter
Common mistake
Reporting only the first failure, so users fix one problem at a time.
Try this
Change the password until it prints Strong password.
Source
examples/ex-015-04-password-feedback.js

08Guided exercises

Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.

Fix the boundary

Correct an off-by-one comparison.

// Rule: orders of 50 or more ship free.
const total = 50;
if (total > 50) {
  console.log("Free shipping");
} else {
  console.log("Shipping: 4.99");
}

Library e-book loan

Complete a decision with several named rules.

const MAX_LOANS = 5;
const activeLoans = 3;
const accountActive = true;
const finesOwed = 2;
// TODO: underLimit, noFines, then decide
let decision = "";
console.log(decision);

Delivery time estimator

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.

const MIN = 10;
const values = [9, 10, 11];
console.log(values[0] >= MIN, values[1] >= MIN, values[2] > MIN);

What is printed?

10Debugging investigation

Exactly 60 minutes is charged as 2 hours

Parking costs $3 per started hour. A driver who parks for exactly 60 minutes is charged $6.

const RATE = 3;
const minutes = 60;
const hours = Math.floor(minutes / 60) + 1;
console.log(`Fee: $${hours * RATE}`);

More cases in the Debugging Detective Laboratory

11Practical scenario

From rules to code

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

Download the whole lesson folder (examples, exercises, solutions, tests) as ZIP

14Source files

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.

16AI learning prompts

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 explanation Boundary 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.
Planning Rules 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.
Debugging Off 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 generation Decision 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 review Magic 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.