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
4 Answers
Reset to default 3Use 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
版权声明:本文标题:javascript - Trying to make function keep running until a condition is met - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745596525a2665514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论