CodeBlanks Logo Icon CodeBlanks

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.

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

Starter code

index.js
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"));

// Add the summary method
// write here

// Call summary and print the result
// write here

Sample solution

index.js
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());

Expected output

Hazır
Merhaba
10
15.03.2024
Hazır | 10 | 15.03.2024

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each method to its output

Term Meaning
capitalize("hello") Hello
clamp(15, 0, 10) 10
formatDate("2024-03-15") 15.03.2024
summary() All together

2 What does the summary method do?

index.js
helpers.summary()
  • helpers nesnesini siler
  • Only calls capitalize
  • Calls all helpers and joins them into one string Correct answer
  • Adds a new helper

Explanation: summary combines all helpers.

3 Complete the summary call

Question
console.log(helpers.____());
Answer
console.log(helpers.summary());

Explanation: summary is called and its result is printed.

All lessons in Intermediate