2017-06-06 176 views
0

我希望我的程序應該像下面這樣工作:它應該一直向用戶提供答案,直到正確的答案和最後的警報消息完成。Javascript while while循環終止而不執行語句

任何幫助,爲什麼它不是這樣woking?

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'); 
    } 
} 
+0

'prompt'&'alert' =阻止用戶界面。 – Tushar

回答

0

這種情況if(answer === 'islamabad')永遠不會爲真:要檢查哪些檢查if != islamabad

while(answer != 'islamabad'){ 
    if(answer === 'islamabad'){ ◄ this will never be true 

嘗試使用以下條件後if == islamabad

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