Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js prompt-sync repeats prompt when using newline characters

I'm working on a JavaScript assignment that requires me to use prompt-synch in Node.JS. It works fine until I try to use a newline character \n within the prompt, at which point every character or backspace typed causes the prompt to repeat itself.

What could I do to get the user input to appear on a new line (a requirement of this exercise) without this issue?

Problem code:

if (guess < answer) {
  guess = prompt("Too low!\n> ");
} else if (guess > answer) {
  guess = prompt("Too high!\n> ");
}

output screenshot

like image 432
Taylour Avatar asked Mar 21 '26 21:03

Taylour


1 Answers

You can try this instead.

if (guess < answer) {
  console.log("Too low!");
  guess = prompt("> ");
} else if (guess > answer) {
  console.log("Too high!");
  guess = prompt("> ");
}

If this does not work then it is likely an issue with another part of your own code. If it does, then it is likely an issue with the prompt-sync module, which makes this a valid workaround if you believe it is.

like image 83
Grant Gryczan Avatar answered Mar 23 '26 11:03

Grant Gryczan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!