+ 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; }); }
7 Antworten
+ 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.
+ 1
I didn't mean that bro sorry
+ 1
Hmm I am using back end JavaScript called Node JS
+ 1
I know that
oh lol, you weren't saying it to me
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();
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>
0
How do I collect user input like an int in javascript. 🙏🏽🙏🏽🙏🏽