11 4 receiving function arguments

Solutions on MaxInterview for 11 4 receiving function arguments by the best coders in the world

showing results for - "11 4 receiving function arguments"
Luca
25 Feb 2020
1/*The logError function outputs a standardized error message to a 
2location determined by the parameter logger.*/
3
4let fileLogger = function(msg) {
5
6   // Put the message in a file
7
8}
9
10function logError(msg, logger) {
11   let errorMsg = 'ERROR: ' + msg;
12   logger(errorMsg);
13}
14
15logError('Something broke!', fileLogger);
16
17
18/*Let's examine this example in more detail.
19
20There are three main program components:
21
22Lines 1-5 define fileLogger, which takes a string argument, msg. 
23We have not discussed writing to a file, but Node.js is capable of 
24doing so.
25
26Lines 7-10 define logError. The first parameter is the message to be 
27logged. The second parameter is the logging function that will do the
28work of sending the message somewhere. logError doesn't know the 
29details of how the message will be logged. It simply formats the 
30message, and calls logger.
31
32Line 12 logs an error using the fileLogger.
33This is the flow of execution:
34
35logError is called, with a message and the logging function fileLogger 
36passed as arguments.
37logError runs, passing the constructed message to logger, which 
38refers to fileLogger.
39fileLogger executes, sending the message to a file.*/
Simone
23 Jan 2020
1/*Our first example will be a generic input validator. It asks the user
2for some input, using the prompt parameter for the text of the 
3question. A second parameter receives a function that does the actual
4work of validating the input.*/
5
6const input = require('readline-sync');
7
8function getValidInput(prompt, isValid) {
9
10   // Prompt the user, using the prompt string that was passed
11   let userInput = input.question(prompt);
12
13   // Call the boolean function isValid to check the input
14   while (!isValid(userInput)) {
15      console.log("Invalid input. Try again.");
16      userInput = input.question(prompt);
17   }
18
19   return userInput;
20}
21
22// A boolean function for validating input
23let isEven = function(n) {
24   return Number(n) % 2 === 0;
25};
26
27console.log(getValidInput('Enter an even number:', isEven));
28
29/*Sample Output: 
30Enter an even number: 3
31Invalid input. Try again.
32Enter an even number: 5
33Invalid input. Try again.
34Enter an even number: 4
354
36
Tom
22 Feb 2016
1/*This example can be made even more powerful by enabling multiple 
2loggers.
3
4Example:
5The call to logError will log the message to both the console and a
6file.*/
7
8let fileLogger = function(msg) {
9
10   // Put the message in a file
11
12}
13
14let consoleLogger = function(msg) {
15
16   console.log(msg);
17
18}
19
20function logError(msg, loggers) {
21
22   let errorMsg = 'ERROR: ' + msg;
23
24   for (let i = 0; i < loggers.length; i++) {
25      loggers[i](errorMsg);
26   }
27
28}
29
30logError('Something broke!', [fileLogger, consoleLogger]);
31
Alessandro
06 Sep 2017
1/*This example uses the same getValidInput function defined above with
2a different prompt and validator function. In this case, we check that
3a potential password has at least 8 characters*/
4
5const input = require('readline-sync');
6
7function getValidInput(prompt, isValid) {
8
9   let userInput = input.question(prompt);
10
11   while (!isValid(userInput)) {
12      console.log("Invalid input. Try again.");
13      userInput = input.question(prompt);
14   }
15
16   return userInput;
17}
18
19let isValidPassword = function(password) {
20
21   // Passwords should have at least 8 characters
22   if (password.length < 8) {
23      return false;
24   }
25
26   return true;
27};
28
29console.log(getValidInput('Create a password:', isValidPassword));
30
31/*Sample Output
32Create a password: launch
33Invalid input. Try again.
34Create a password: code
35Invalid input. Try again.
36Create a password: launchcode
37launchcode