2016-11-15 20 views
2

JavaScript相當新穎如果用戶輸入錯誤答案並且我想讓問題重複,直到他們得到正確答案,我該如何循環回提示問題。如果回答不正確,我該如何循環回提示符javascript

<html> 
 

 
<head> 
 
    <script> 
 
    </script> 
 
    <title> Javascript program</title> 
 
</head> 
 

 
<body> 
 
    <script> 
 
     var company = (prompt("What the name of the company that developed the javascript language?", "")); 
 
     if (company == 'netscape') { 
 
      alert("correct answer!"); 
 

 
     } else { 
 
      alert("wrong answer"); 
 

 
     } 
 
    </script> 
 
</body> 
 

 
</html>

+0

看一看a ['while'](https://developer.mozilla。org/en-US/docs/Web/JavaScript/Reference/Statements/while)循環。順便說一句,如果有人輸入「Netscape」而不是「netscape」,會發生什麼情況。 – Tigger

+0

要添加到答案,你也可以使用do while循環[如此處所示](http://stackoverflow.com/questions/15047140/javascript-prompt-number-and-continue-prompting-if-answer-is -wrong#answer-15047174) –

回答

2

您可以在一個功能包裝你的代碼,並呼籲不正確的值的自我。

var max_count = 5; 
 

 
function showConfirm() { 
 
    var company = (prompt("What the name of the company that developed the javascript language?", "")); 
 
    if (company == 'netscape') { 
 
    alert("correct answer!"); 
 
    } else { 
 
    alert("wrong answer"); 
 
    // to limit user for limited count 
 
    if (--max_count > 0) 
 
     showConfirm() 
 
    } 
 
} 
 
showConfirm();

0

你可以使用一個循環,像這樣:

let question = "What is the name of the company that developed the javascript language?", 
    defaultAnswer = "stackoverflow", 
    correctAnswer = "netscape"; 

while(prompt(question, defaultAnswer) !== correctAnswer) alert("Wrong answer") 
alert("Correct answer"); 

或者你可以使用遞歸,請參閱Rajesh的答案。

1

您可以永久使用while循環並循環,直到條件爲true,然後使用break語句退出循環。

基本上,可以選擇循環,檢查的條件第一

while (condition) { 
    // code 
} 

環,其檢查的條件以後

do { 
    // code 
} while (condition); 

溶液其間

while (true) { 
    // code 
    if (condition) break; 
    // code 
} 

或遞歸解決方案,像

function fn() { 
    // code 
    if (condition) { 
     // code 
     return 
    } 
    fn(); // call fn again 
}   

但我建議使用迭代方法,直到值滿足條件。

var company; 
 

 
while (true) { 
 
    company = prompt("What the name of the company that developed the javascript language?", ""); 
 
    if (company === 'netscape') { 
 
     break; 
 
    } 
 
    alert("wrong answer"); 
 
} 
 
alert("correct answer!");

0

你需要包括功能本身,直到符合要求和返回(退出功能);

ask(); 
function ask(){ 
    var answer=prompt("Question..?"); 
    if(answer=="netscape"){ 
     alert("correct"); 
     return; 
    } 
    alert("wrong answer"); 
    ask(); 
} 
+0

你不需要'else'並且'ask()'應該在定義之後。 – Rajesh

+0

是的,它甚至沒有其他區塊整潔 – Chase

0

呈現不斷提示可以刺激和用戶總是有選擇的防止網頁創造更多對話。如果您使用while(true)的循環,並且用戶選擇防止對話過度,那麼您的頁面最終可能處於掛起狀態。我會建議使用計數器假設答案之前要求的時間特定數量的問題是錯誤的,如下面的代碼

function askQuestion() { 
    return prompt("What the name of the company that developed the javascript language?", ""); 
} 

var counter = 0; 
var correctAnswer = false; 
while(!correctAnswer && counter < 10) { 

    counter++; 
    correctAnswer = askQuestion() === 'netscape'; 

    if (!correctAnswer) { 
     alert("wrong"); 
    } 
} 

if (correctAnswer) { 
    alert("correct"); 
} else { 
    alert("sorry after 10 attempts answer is still wrong"); 
} 

您還可以檢查此plnkr鏈接https://plnkr.co/edit/IbUFRC0HepfSHlvNqMzM?p=preview

相關問題