5

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.

enter image description here

How to fix this issue, and why does it happen?

3
  • what are your key strokes when entering the values? Perhaps something is counted as an input or "return" that you are not expecting. Commented Nov 15, 2015 at 1:04
  • @clay simply the number and then I press enter. I also tried just to press enter, the result is the same. Commented Nov 15, 2015 at 3:26
  • Are you on Windows? You might be running into this issue: github.com/flatiron/prompt/issues/131 Commented Nov 15, 2015 at 19:33

1 Answer 1

3

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
  });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.