11.4. Receiving Function Arguments

/*The logError function outputs a standardized error message to a 
location determined by the parameter logger.*/

let fileLogger = function(msg) {

   // Put the message in a file

}

function logError(msg, logger) {
   let errorMsg = 'ERROR: ' + msg;
   logger(errorMsg);
}

logError('Something broke!', fileLogger);


/*Let's examine this example in more detail.

There are three main program components:

Lines 1-5 define fileLogger, which takes a string argument, msg. 
We have not discussed writing to a file, but Node.js is capable of 
doing so.

Lines 7-10 define logError. The first parameter is the message to be 
logged. The second parameter is the logging function that will do the
work of sending the message somewhere. logError doesn't know the 
details of how the message will be logged. It simply formats the 
message, and calls logger.

Line 12 logs an error using the fileLogger.
This is the flow of execution:

logError is called, with a message and the logging function fileLogger 
passed as arguments.
logError runs, passing the constructed message to logger, which 
refers to fileLogger.
fileLogger executes, sending the message to a file.*/

3
1
Lolol 110 points

                                    /*Our first example will be a generic input validator. It asks the user
for some input, using the prompt parameter for the text of the 
question. A second parameter receives a function that does the actual
work of validating the input.*/

const input = require('readline-sync');

function getValidInput(prompt, isValid) {

   // Prompt the user, using the prompt string that was passed
   let userInput = input.question(prompt);

   // Call the boolean function isValid to check the input
   while (!isValid(userInput)) {
      console.log("Invalid input. Try again.");
      userInput = input.question(prompt);
   }

   return userInput;
}

// A boolean function for validating input
let isEven = function(n) {
   return Number(n) % 2 === 0;
};

console.log(getValidInput('Enter an even number:', isEven));

/*Sample Output: 
Enter an even number: 3
Invalid input. Try again.
Enter an even number: 5
Invalid input. Try again.
Enter an even number: 4
4

3 (1 Votes)
0
3.5
6

                                    /*This example uses the same getValidInput function defined above with
a different prompt and validator function. In this case, we check that
a potential password has at least 8 characters*/

const input = require('readline-sync');

function getValidInput(prompt, isValid) {

   let userInput = input.question(prompt);

   while (!isValid(userInput)) {
      console.log("Invalid input. Try again.");
      userInput = input.question(prompt);
   }

   return userInput;
}

let isValidPassword = function(password) {

   // Passwords should have at least 8 characters
   if (password.length < 8) {
      return false;
   }

   return true;
};

console.log(getValidInput('Create a password:', isValidPassword));

/*Sample Output
Create a password: launch
Invalid input. Try again.
Create a password: code
Invalid input. Try again.
Create a password: launchcode
launchcode

3.5 (6 Votes)
0
4
1

                                    /*This example can be made even more powerful by enabling multiple 
loggers.

Example:
The call to logError will log the message to both the console and a
file.*/

let fileLogger = function(msg) {

   // Put the message in a file

}

let consoleLogger = function(msg) {

   console.log(msg);

}

function logError(msg, loggers) {

   let errorMsg = 'ERROR: ' + msg;

   for (let i = 0; i < loggers.length; i++) {
      loggers[i](errorMsg);
   }

}

logError('Something broke!', [fileLogger, consoleLogger]);

4 (1 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source