skip to Main Content
let checkOrFold = undefined
while(checkOrFold != 'check' && checkOrFold != 'fold'){
    let checkOrFold = prompt(`check or fold?`);
    console.log(checkOrFold != 'check' && checkOrFold != 'fold')
}
console.log('finish') here

even the console is logging false but prompt keeps popping up. What could be wrong?

Tried debugging. When ‘check’ or ‘fold’ is typed, console.log should show finish.

3

Answers


  1. try matching the response till you get either check or fold as a response
    while(checkOrFold = ‘check’ || checkOrFold = ‘fold’)

    Login or Signup to reply.
  2. You’re declaring another variable with the same name in the while loop. Remove the let from within the while loop and you should get what you’re looking for.

    let checkOrFold = undefined;
    while(checkOrFold != 'check' && checkOrFold != 'fold'){
        checkOrFold = prompt(`check or fold?`);
        console.log(checkOrFold != 'check' && checkOrFold != 'fold');
    }
    console.log('finish');
    
    Login or Signup to reply.
  3. You probably want to prompt outside the while loop once, so checkOrFold has a chance to receive an input. You also, by accident, setup a separate block scoped variable checkOrFold inside the loop, you need to use the same checkOrFold for the whole portion of code to work.

    Solution:

    let checkOrFold = undefined;
    
    checkOrFold = prompt(`check or fold?`);
    while(checkOrFold != 'check' && checkOrFold != 'fold'){
        checkOrFold = prompt(`check or fold?`);
        console.log(checkOrFold != 'check' && checkOrFold != 'fold')
    }
    console.log('finish')
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search