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
/*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
Thank you!
1
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
Thank you!
6
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]);
Thank you!
1
0
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line