Pure Programmer
Blue Matrix


Cluster Map

Console Input

L1

This page is under construction. Please come back later.

ConsoleInput1.js
#!/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 () => {
	const NAME = await Utils.prompt("What is your name? ");
	const FAVORITE_COLOR = await Utils.prompt("What is your favorite color? ");
	console.log(Utils.format("Hello, {0:s}!  I like {1:s} too!", NAME, FAVORITE_COLOR));
}
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!
ConsoleInput2.js
#!/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 favoriteInt = 0;
	let favoriteLong = 0;
	let favoriteDouble = 0.0;
	try {
		const FAVORITE_INT_INPUT = await Utils.prompt("What is your favorite small integer? ");
		favoriteInt = Utils.stoi(FAVORITE_INT_INPUT);
		const FAVORITE_LONG_INPUT = await Utils.prompt("What is your favorite large integer? ");
		favoriteLong = Utils.stoi(FAVORITE_LONG_INPUT);
		const FAVORITE_DOUBLE_INPUT = await Utils.prompt("What is your favorite floating point? ");
		favoriteDouble = Utils.stod(FAVORITE_DOUBLE_INPUT);
	} catch (ex) {
		if (ex instanceof Utils.NumberFormatError) {
			console.log(["Bad input! ", String(ex)].join(''));
		} else if (ex instanceof Error) {
			console.log("Don't know what went wrong!");
		}
	}
	const SUM = favoriteInt + favoriteLong + favoriteDouble;
	console.log(Utils.format("All together they add up to {0:f}!", SUM));
}
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

Projects

More ★'s indicate higher difficulty level.

References