In the file 0-console.js, create a function named displayMessage that prints in STDOUT the string argument.bob@dylan:~$ cat 0-main.jsconst displayMessage = require('./0-console');displayMessage("Hello NodeJS!");bob@dylan:~$ node 0-main.jsHello NodeJS!bob@dylan:~$
Question
In the file 0-console.js, create a function named displayMessage that prints in STDOUT the string argument.bob@dylan:~ node 0-main.jsHello NodeJS!bob@dylan:~$
Solution
Here is how you can create a function named displayMessage that prints in STDOUT the string argument in the file 0-console.js.
-
Open the file
0-console.jsin your text editor. -
Write the following code in the file:
function displayMessage(message) {
console.log(message);
}
module.exports = displayMessage;
In this code, we define a function displayMessage that takes one argument message. Inside the function, we use console.log to print the message to STDOUT. Finally, we export the function using module.exports so that it can be imported and used in other files.
-
Save the file and close it.
-
Now, you can use the
displayMessagefunction in your0-main.jsfile as shown in your question. When you runnode 0-main.js, it will print "Hello NodeJS!" to STDOUT.
Similar Questions
Which method is commonly used to log messages to the console in JavaScript?a) console.print()console.write()console.log()console.display()
What will be the output of the following code in the console?File: my_module.js exports.name = 'Zeus'; Code: var my_module = require('./mymodule'); console.log((function(settings){ return settings.split('').reverse().join('') })(my_module.name)); (1 Point)ErrorsueZundefinedZeus
create a class Greet that reads in from the console a string, and then outputs a greeting Hi ...!, where the name replaces the elipses. e.g. If the input is Hugh then your output should be Hi Hugh!.
How can you print what is stored in a JavaScript variable?(1 Point)log(variable)console.log(variable)print(variable)extract(variable)
Which function is used to print a message on the console in C++?Select one:cout<<;cin>>;Printf()printf();
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.