+ 2

JavaScript console input and store as a variable.

I have created a JavaScript console input code and tried to return the value but it showed "undefined" //Here's what I've tried var a = input(); function input(){ process.stdin.resume(); process.stdin.setEncoding("ascii"); process.stdin.on("data", function(value){ return value; }); }

10th Aug 2019, 5:32 AM
CoderBittu
CoderBittu - avatar
7 Answers
+ 2
Ok. Then here is an example of input in Node.js from the console. const readline = require('readline); const userInput = readline.createInterface({ input: process.stdin, output: process.stdout }); userInput.question('What is your favorite number? ', (favNum) => { let num = favNum; console.log(`Thank you, your favorite number is: ${favNum}`); userInput.close(); }); On a windows 10 machine in the cmd type node enter, then type .editor enter and when finished writing the code type ctrl + d. It will ask what is your favorite number. But, because you are typing input from the same cmd it shows each digit twice but, gives the correct output.
10th Aug 2019, 12:21 PM
Shawn Gillis
Shawn Gillis - avatar
+ 1
I didn't mean that bro sorry
10th Aug 2019, 7:05 AM
CoderBittu
CoderBittu - avatar
+ 1
Hmm I am using back end JavaScript called Node JS
10th Aug 2019, 10:11 AM
CoderBittu
CoderBittu - avatar
+ 1
I know that oh lol, you weren't saying it to me
10th Aug 2019, 11:03 AM
RZK 022
RZK 022 - avatar
0
If you tried this on Sololearn..... Unfortunately it's not gonna work if not, for input as far as I know we use : let variable = console.readline();
10th Aug 2019, 8:32 AM
RZK 022
RZK 022 - avatar
0
Subhajeet Sikdar ... In JavaScript input is obtained with the prompt() function. Here is an example using HTML and a function that uses the document.get ElementBy Id().innerHTML Dom function. <!DOCTYPE html> <html> <body> <!--A Simple JS Function: Declararion, Invocation and Return that will call a function which prompts user input then performs a calculation and returns the result.--> <p id="demo"></p> <script> var _$x = myFunction(); document.getElementById("demo").innerHTML = _$x; function myFunction(a) { a = prompt("Enter a number to find its Squareroot"); let b = Math.sqrt(a) ;     return "The squareroot of the number you entered is " + b; } </script> </body> </html>
10th Aug 2019, 9:27 AM
Shawn Gillis
Shawn Gillis - avatar
0
How do I collect user input like an int in javascript. 🙏🏽🙏🏽🙏🏽
19th Mar 2021, 4:38 PM
DURU SOMTOCHUKWU PHILIP
DURU SOMTOCHUKWU PHILIP - avatar