对 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>
最佳答案
您可以将您的代码包装在一个函数中,并在不正确的值上调用 self。
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();
关于javascript - 如果答案不正确,我如何循环回到提示 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40606697/