How to use the keyInYN function from readline-sync

Find comprehensive JavaScript readline-sync.keyInYN code examples handpicked from public code repositorys.

readline-sync.keyInYN is a synchronous method in Node.js that reads input from the user and returns a Boolean value based on whether the user entered "y" or "n".

13
14
15
16
17
18
19
20
21
22
console.log(JSON.stringify(config.database, null, 2))
await sleep(200)
answer = readLine.keyInYN('Is it correct?')
if (!answer) return console.log('Rectify your config file and then restart this installer')

answer = readLine.keyInYN('Do you want to erase all database content?')
console.log('Starting the migration...')
let sucess = await dbSync(answer)
if (!sucess) return
answer = readLine.keyInYN('Do you want to add a new user?')
fork icon2
star icon7
watch icon3

+ 55 other calls in file

22
23
24
25
26
27
28
29
30
31
32
33
${chalk.yellow(tools.getConfigDir())} (to change this, set your \
${chalk.grey("XDG_CONFIG_HOME")} environment variable).`);


    const sendEmail = rl.questionEMail("Sender account email: ");
    const sendPass = rl.question("Sender account password: ", tools.passOpts);
    const isSame = rl.keyInYN("Are the sender and receiver accounts the same?");


    getRecvCreds(isSame, sendEmail, sendPass, (recvEmail, recvPass) => {
        const shouldStore = askToStore();

fork icon0
star icon8
watch icon0

How does readline-sync.keyInYN work?

readline-sync.keyInYN() is a function in the readline-sync package that prompts the user to enter a "yes" or "no" answer by pressing "Y" or "N" on the keyboard and returns true if the user enters "Y" and false if the user enters "N". It also allows for customization of the message prompt and default value.

155
156
157
158
159
160
161
162
163
164

goodStrike();
function checkSunkShips() {
	if (hitPoint === 0) {
		console.log("You have sunk all my ships!!!");
		let playAgain = rls.keyInYN("Would you like to play again? ");
		if (playAgain === true) {
			game();
		} else {
			console.log("Have a good day!!");
fork icon0
star icon0
watch icon1

+ 3 other calls in file

60
61
62
63
64
65
66
67
68
69
function checkSunkShips() {
	if (sunkShips.length === 1) {
		console.log("You sunk my BattleShip! 1 ship remaining!!");
	} else if (sunkShips.length === 2) {
		console.log("You've sunk all my Battleships!!");
		let playAgain = rls.keyInYN("Would you like to play again?");
		if (playAgain === true) {
			game();
		} else {
			console.log("Have a good Day.");
fork icon0
star icon0
watch icon1

+ 8 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const readlineSync = require("readline-sync");

const answer = readlineSync.keyInYN("Do you want to proceed?");

if (answer) {
  console.log("Great, let's proceed!");
} else {
  console.log("Okay, maybe some other time.");
}

In this example, the keyInYN() method is called with a string argument that will be displayed to the user as the question to answer. The user can input "y" for "yes" or "n" for "no". The function returns true if the user answered "y" and false if the user answered "n".

-2
fork icon0
star icon0
watch icon1

+ 19 other calls in file

0
1
2
3
4
5
6
7
8
9
"use strict";
var readlineSync = require("readline-sync");
var fs = require("fs");
// Function to delete .env file
const envDelete = () => {
  let ans = readlineSync.keyInYN(`Do you want to delete .env file?`);
  console.log(ans);
  if (ans) {
    try {
      fs.unlinkSync("./.env");
fork icon0
star icon0
watch icon0