I'm currently taking a course on Coursera, and doing an exercise using node.js code to calculate a quadratic expression. All the code is given and this exercise is merely to get us to know node.js, but still I'm encountering a problem entering a prompt.
the code is here:
var quad = require('./quadratic');
var prompt = require('prompt');
prompt.get(['a', 'b', 'c'], function (err, result) {
if (err) { return onErr(err); }
console.log('Command-line input received:');
console.log('a: ' + result.a);
console.log('b: ' + result.b);
console.log('c: ' + result.c);
quad(result.a,result.b,result.c, function(err,quadsolve) {
if (err) {
console.log('Error: ', err);
}
else {
console.log("Roots are "+quadsolve.root1() + " " + quadsolve.root2());
}
});
});
As you see, I'm using the prompt
module, but when I enter the input for a
, the cmd is skipping the input for b
and requesting me to enter `c, which in turn of couse, resulting in an error.
How to fix this issue, and why does it happen?
Welcome to developing on windows! Windows uses a carriage return in addition to a \n
line ending which is probably why you see this bug. You can force prompt to tokenize on a regular expression like this, which should hopefully fix your issue:
var schema = {
properties: {
a: { pattern: /^[0-9]+$/, message: 'a', required: true },
b: { pattern: /^[0-9]+$/, message: 'b', required: true },
c: { pattern: /^[0-9]+$/, message: 'c', required: true }
}
};
prompt.get(schema, function (err, result) {
// .. rest of your code
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With