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

♀尐吖头ヾ 提交于 2019-12-13 03:50:03

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/50770848/node-js-prompt-sync-repeats-prompt-when-using-newline-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!