Console Input
This page is under construction. Please come back later.
#!/usr/bin/env node; /****************************************************************************** * This program demonstrates how to prompt the user for input. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ const Utils = require('./Utils'); const main = async () => { let name; name = await Utils.prompt("What is your name? "); let favoriteColor; favoriteColor = await Utils.prompt("What is your favorite color? "); console.log(Utils.format("Hello, {0:s}! I like {1:s} too!", name, favoriteColor)); } main().catch( e => { console.error(e) } );
Output
$ node ConsoleInput1.js < ../../examples/ConsoleInput1.in
What is your name? Rich
What is your favorite color? blue
Hello, Rich! I like blue too!
#!/usr/bin/env node; /****************************************************************************** * This program demonstrates how to prompt the user for input. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ const Utils = require('./Utils'); const main = async () => { try { let favoriteInt; let favoriteLong; let favoriteDouble; let favoriteIntInput; favoriteIntInput = await Utils.prompt("What is your favorite small integer? "); favoriteInt = Utils.stoi(favoriteIntInput); let favoriteLongInput; favoriteLongInput = await Utils.prompt("What is your favorite large integer? "); favoriteLong = Utils.stoi(favoriteLongInput); let favoriteDoubleInput; favoriteDoubleInput = await Utils.prompt("What is your favorite floating point? "); favoriteDouble = Utils.stod(favoriteDoubleInput); const sum = favoriteInt + favoriteLong + favoriteDouble; console.log(Utils.format("All together they add up to {0:f}!", sum)); } catch (ex) { if (ex instanceof Utils.NumberFormatError) { console.log("Bad input! " + String(ex)); } else if (ex instanceof Error) { console.log("Don't know what went wrong!"); } } } main().catch( e => { console.error(e) } );
Output
$ node ConsoleInput2.js < ../../examples/ConsoleInput2.in1
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? 3.141926
All together they add up to 1000329.1415926!
$ node ConsoleInput2.js < ../../examples/ConsoleInput2.in2
What is your favorite small integer? 326
What is your favorite large integer? abc
What is your favorite floating point? 3.141926
Bad input!
$ node ConsoleInput2.js < ../../examples/ConsoleInput2.in3
What is your favorite small integer? 326
What is your favorite large integer? 1000000
What is your favorite floating point? abc
Bad input!
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[JavaScript Language Reference]], Mozilla Developer Network
- [[Mozilla Developer Network]]
- Download [[node.js]]
- [[w3schools.com]]