最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Trying to make function keep running until a condition is met - Stack Overflow

matteradmin7PV0评论

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();

Post a comment

comment list (0)

  1. No comments so far