Node.js
Putting it together
Combine helper functions into a module.
Putting it together
In the final lesson we will add a summary method that combines all helper methods. This method calls capitalize, clamp and formatDate and joins their results in a single string.
Example: helpers.summary = function() { return helpers.capitalize("ready") + " | " + helpers.clamp(15, 0, 10) + " | " + helpers.formatDate("2024-03-15"); }. Call: helpers.summary() returns Ready | 10 | 15.03.2024. This method shows all the library's tools from one place, so you see how the helpers work together. We take the previous lessons' solution and add this method, completing the library.
Summary design
The summary method takes no parameters; it calls all values as hardcoded constants and joins them with the " | " separator. This is a demo method that shows how the library works at a glance.
Flow: helpers.summary = function() { return helpers.capitalize("ready") + " | " + helpers.clamp(15, 0, 10) + " | " + helpers.formatDate("2024-03-15"); }. The call console.log(helpers.summary()) prints Ready | 10 | 15.03.2024. This appears on a new line after the previous outputs (Ready, Hello, 10, 15.03.2024). This way all five lessons' output has accumulated and the helper library is complete.
Exercise Add the summary method and call it
Take the previous lessons' solution. Add the helpers.summary method with body return helpers.capitalize("ready") + " | " + helpers.clamp(15, 0, 10) + " | " + helpers.formatDate("2024-03-15");. Print the result with console.log(helpers.summary()).
Show example solution
const helpers = {};
module.exports = helpers;
console.log("Ready");
helpers.capitalize = function(text) {
if (!text) return text;
return text[0].toUpperCase() + text.slice(1);
};
console.log(helpers.capitalize("hello"));
helpers.clamp = function(value, min, max) {
if (value < min) return min;
if (value > max) return max;
return value;
};
console.log(helpers.clamp(15, 0, 10));
helpers.formatDate = function(iso) {
const [y, m, d] = iso.split("-");
return d + "." + m + "." + y;
};
console.log(helpers.formatDate("2024-03-15"));
helpers.summary = function() {
return helpers.capitalize("ready") + " | " + helpers.clamp(15, 0, 10) + " | " + helpers.formatDate("2024-03-15");
};
console.log(helpers.summary());
Test yourself
-
Match each method to its output
- capitalize("hello")
- Hello
- clamp(15, 0, 10)
- 10
- formatDate("2024-03-15")
- 15.03.2024
- summary()
- All together
-
What does the summary method do?
helpers.summary()
- helpers nesnesini siler
- Only calls capitalize
- Calls all helpers and joins them into one string
- Adds a new helper
Show answer
Calls all helpers and joins them into one string — summary combines all helpers.
-
Complete the summary call
console.log(helpers.____());
Show answer
console.log(helpers.summary());
Next lesson: What is Express?