Learning objective: Break a problem into small single-purpose functions, compose them so one function's output feeds the next, test each function, and assemble a small application.
Skills practised: Functions, Decomposition and composition, Application design.
04Explanation
Decomposition means splitting a problem into small steps and giving each step its own function: parse input, validate, calculate, format, display.
Small functions are easier to name, test and reuse. If a function's description needs the word "and", consider splitting it.
Composition means feeding one function's return value into the next: money(tipFor(toNumber(text), 15)). Store intermediate results in well-named variables when a chain gets long.
Test each function on its own with a few inputs before connecting them. When the full program misbehaves, you can then trust the pieces and look at how they are joined.
Separate pure core logic (calculations, validation) from the shell that touches the outside world (DOM, console, storage). The core can be unit tested without a browser.
Return result objects such as { ok: false, error: "..." } from validation so callers can decide how to display problems instead of throwing for expected user errors.
Keep constants such as tax rates in one configuration object passed to functions rather than read from globals; this makes testing different configurations trivial.
A pipe helper (const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x)) composes functions left to right; use it when a chain of single-argument transformations becomes common.
05Definitions
Decomposition
Breaking a problem into smaller parts, each solved by its own function.
Composition
Combining functions so the output of one becomes the input of the next.
Single responsibility
A function should have one clear job.
Helper function
A small function that supports a larger task.
Unit test
A check that one function returns the expected value for a given input.
The Interactive Tip Calculator project uses exactly this structure: parse the form, validate, calculate, format and render. Each function is tested separately, so the interface code stays small.
const half = n => n / 2;
const add1 = n => n + 1;
console.log(add1(half(20)));
11
12
10
22
2. What is decomposition?
Deleting code
Splitting a problem into smaller functions
Running code faster
Combining files
3. Why is the result NaN?
const format = n => "$" + n.toFixed(2);
const triple = n => n * 3;
console.log(triple(format(2)));
format returns a string that is then multiplied
Number is wrong
Arrow functions cannot chain
toFixed is missing
4. Why test helper functions separately?
It is required by JavaScript
A failure points to one small piece
It makes code longer on purpose
It hides bugs
5. Which function has a single responsibility?
readFormCalculateAndShow
calculateTip
doEverything
handleAllTheThings
6. Where should console.log live in a well-structured program?
Inside every helper
At the edge, after the core functions return values
Nowhere
In validation only
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 pipeline order: Prints $15.00
Split the bill: Prints 23.00; tipAmount works; perPerson uses tipAmount
Mini tip calculator: Prints all three results; calculate is pure and correct; validate rejects zero people; Four helper functions
ex-025-01: output matches the documented expected output
ex-025-02: output matches the documented expected output
ex-025-03: output matches the documented expected output
ex-025-04: output matches the documented expected output
The lesson lives in the companion repository at curriculum/level-01-foundations/module-05-functions-and-reusable-logic/lesson-025-combining-functions-into-applications/. Every example on this page is generated from those files.
The GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Academy) has not been published yet, so these links will work once it is. Until then, use the ZIP download above.
15Summary and next steps
Decompose: one small function per step.
Compose: pass return values from one function to the next.
Validate with result objects; print only at the edges.
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 explanationDecomposition
Context: I can write functions.
Task: Show me how to split a tip calculator into five small functions and explain each.
Constraints: Beginner level.
Expected behaviour: I can name the functions myself next time.
Testing: Give me another app to decompose.
Code predictionComposition drill
Context: I know how to combine functions.
Task: Give me five nested function calls and ask for the result.
Constraints: Wait for answers.
Expected behaviour: Include one where order changes the result.
Testing: Explain inside-out evaluation.
DebuggingPipeline bug
Context: My composed functions return NaN.
Task: Help me test each step to find where it breaks.
Constraints: No direct answer.
Expected behaviour: I find the step myself.
Testing: Suggest a test for each helper.
Exercise generationMini applications
Context: I finished the mini tip calculator.
Task: Create one more mini application specification that needs parse, validate, calculate and format functions.
Constraints: Fictional data, exact output.
Expected behaviour: Include three inputs, one invalid.
Testing: Include expected output.
Code reviewFunction design
Context: Here is my mini application.
Task: Review function names, responsibilities and where printing happens.
Constraints: Do not rewrite everything.
Expected behaviour: Each function has one job.
Testing: List the top three improvements.