Node.js prompt to skip input

I'm currently heading for Coursera and doing an exercise using node.js code to compute a quadratic expression. All the code is listed, and this exercise is just to introduce us to node.js, but still I ran into a problem that comes in the invitation.
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 can see, I use the prompt module, but when I enter the input for a , cmd skips the input for b and asks me to enter `c, which in turn is couse, which leads to an error.

enter image description here

How to solve this problem and why is this happening?

+5
source share
1 answer

Welcome to development on Windows! Windows uses carriage returns in addition to ending the line \n , which is probably the reason you see this error. You can force the tokenize request to a regular expression like this, which hopefully fixes your problem:

  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 }); 
+3
source

Source: https://habr.com/ru/post/1235964/


All Articles