Answers for "11.4. Receiving Function Arguments"

0

11.4. Receiving Function Arguments

/*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
Posted by: Guest on July-08-2021
0

11.4. Receiving Function Arguments

/*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]);
Posted by: Guest on July-08-2021
0

11.4. Receiving Function Arguments

/*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
Posted by: Guest on July-08-2021
0

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.*/
Posted by: Guest on July-08-2021

Code answers related to "11.4. Receiving Function Arguments"

Browse Popular Code Answers by Language