admin管理员组

文章数量:1433462

I am currently using readline sync to track user input. I would like a user to ultimately select yes. If a user selects no, I want to re-ask the question until they choose the yes condition. My code as it is written it will run, and if a user selects no it will call the function and run again, but it stops continuing to run after I select No more than two times. I am wondering what I need to change to ensure my function keeps running until a selection of Yes is made.

const readlineSync = require('readline-sync')

const test = () => {
  const choice = ['YES', 'NO']

  let userInput = readlineSync.keyInSelect(choice)

  return choice[userInput]
}

let solution = test()

if (solution === 'YES') {
  console.log('this will run')
} else {
  console.log('this will then run')
  test()
}

I am currently using readline sync to track user input. I would like a user to ultimately select yes. If a user selects no, I want to re-ask the question until they choose the yes condition. My code as it is written it will run, and if a user selects no it will call the function and run again, but it stops continuing to run after I select No more than two times. I am wondering what I need to change to ensure my function keeps running until a selection of Yes is made.

const readlineSync = require('readline-sync')

const test = () => {
  const choice = ['YES', 'NO']

  let userInput = readlineSync.keyInSelect(choice)

  return choice[userInput]
}

let solution = test()

if (solution === 'YES') {
  console.log('this will run')
} else {
  console.log('this will then run')
  test()
}

Share Improve this question edited Jul 21, 2020 at 4:52 Unmitigated 89.9k12 gold badges99 silver badges104 bronze badges asked Jul 21, 2020 at 4:10 Rob TerrellRob Terrell 2,5624 gold badges21 silver badges48 bronze badges 1
  • You need to put in side a infinite loop and break until user enters yes . This will run once only. – Harmandeep Singh Kalsi Commented Jul 21, 2020 at 4:17
Add a ment  | 

4 Answers 4

Reset to default 3

Use a do...while loop:

let solution;
do {
    solution = test();
} while(solution!=='YES');

You have to create an endless loop of a function until the result you're looking for is received. You can do this by nesting your solution statement in a function and returning the result of the same function forever if the next result is NO, until it's YES, then return YES.

const readlineSync = require('readline-sync')

const test = () => {
  const choice = ['YES', 'NO']

  let userInput = readlineSync.keyInSelect(choice)

  return choice[userInput]
}

const loop = () => {
  let solution = test()

  if (solution === 'YES') {
    console.log('this will run')
    
    return solution;
  } else {
    console.log('this will then run')
    
    return loop()
  }
}

const result = loop();

console.log("result is " + result)

const test = () => {
  const choice = ['YES', 'NO']
  let count = 0
  while(true){  
   let userInput = readlineSync.keyInSelect(choice)
   if(userInput === "YES") break;
   }
  return choice[userInput]
}

while(true) will make loop infinite, it will stop only when the user selects YES and it break out of the loop.

So these situation , where you want to run the program at least once and further decide based on the user input , are mostly handled by using do while loop

const readlineSync = require('readline-sync')

const test = () => {
let userChoice = "";
const choice = ['YES', 'NO']
do{
  
  let userInput = readlineSync.keyInSelect(choice)

  userChoice  = choice[userInput]
  
  if (userChoice === 'YES') {
  console.log('this will run')
 } else {
  console.log('this will then run')
 }
  
  } while(userChoice !== 'YES');
}

test();

本文标签: javascriptTrying to make function keep running until a condition is metStack Overflow