gpt4 book ai didi

javascript - 如果答案不正确,我如何循环回到提示 javascript

转载 作者:行者123 更新时间:2023-11-29 19:09:02 25 4
gpt4 key购买 nike

对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com