From exercises to projects
A project integrates many concepts at once. The key is not knowing more syntax, but breaking down the problem into manageable parts.
A 4-step method
- Understand the problem: what comes in, what goes out, what edge cases are there?
- Split into functions: each function solves a small, testable part.
- Implement the minimum: get the simple case working before the complex ones.
- Test and refactor: add tests and clean up the code.
Example: form validator
Instead of one giant function, separate responsibilities:
function isValidEmail(email) {
return /^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email);
}
function hasMinLength(text, min) {
return text.length >= min;
}
function validateForm(data) {
const errors = [];
if (!isValidEmail(data.email)) errors.push("Invalid email");
if (!hasMinLength(data.password, 8)) errors.push("Password too short");
return { valid: errors.length === 0, errors };
}
Each piece is simple, easy to test and reusable. That is how you build maintainable software.
Examples
Compose small functions
const isPositive = (n) => n > 0;
const isInteger = (n) => Number.isInteger(n);
function validateQuantity(n) {
return isPositive(n) && isInteger(n);
}
console.log(validateQuantity(5));
console.log(validateQuantity(-2));
console.log(validateQuantity(3.5));