Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript while循环终止而不执行语句_Javascript_Loops_If Statement - Fatal编程技术网

Javascript while循环终止而不执行语句

Javascript while循环终止而不执行语句,javascript,loops,if-statement,Javascript,Loops,If Statement,我希望我的程序应该像这样工作:它应该要求用户的答案一遍又一遍,直到正确的答案,并在最后警告做得好的消息 有没有人帮我解释为什么不是那样的 var answer = prompt('What is the capital of Pakistan?'); while(answer != 'islamabad'){ if(answer === 'islamabad'){ alert('Your answer is correct!'); }else if(answer != 'isla

我希望我的程序应该像这样工作:它应该要求用户的答案一遍又一遍,直到正确的答案,并在最后警告做得好的消息

有没有人帮我解释为什么不是那样的

var answer = prompt('What is the capital of Pakistan?');
while(answer != 'islamabad'){
  if(answer === 'islamabad'){
    alert('Your answer is correct!');
  }else if(answer != 'islamabad'){
    answer = prompt('Try again.');
  }else{
    alert('Well Done');
  }
}
此条件
if(答案==='islamabad')
永远不会为真:在检查
if!=伊斯兰堡

while(answer != 'islamabad'){
    if(answer === 'islamabad'){   ◄ this will never be true
尝试使用以下方法:

var answer = prompt('What is the capital of Pakistan?');
while(answer != 'islamabad'){       
    answer = prompt('Try again.');
}
alert('Well Done');

提示
&
警报
=阻止用户界面。