【问题标题】:My javascript while loop stops after the first prompt我的 javascript while 循环在第一个提示后停止
【发布时间】:2013-01-13 15:04:24
【问题描述】:

谁能告诉我为什么我的脚本不会进入第二个问题? 我检查了 Firefox 控制台,它没有显示任何语法错误,所以我有点卡在这里。

提前谢谢你。

这是我的脚本:

<head>
<script type="text/javascript" >
var vraag = new Array();
var cantwoord = new Array();

vraag[1] = "8x4=? ";
vraag[2] = "8/4=? ";
vraag[3] = "16x4=? ";

cantwoord[1]= "32";
cantwoord[2]= "2";
cantwoord[3]= "64";

function toets(index)
{
antwoord = prompt(vraag[index]);
if(antwoord == cantwoord[index])
{
return ("Correct");
}   
else
{
return ("Fout");    
}

}


</script>
</head>

<body>
<script type="text/javascript">
//<![CDATA[

var doorgaan = true;
var index = 0;
while(doorgaan)
{
index++;
resultaat = toets(index);
doorgaan = confirm(reultaat+" Wil je doorgaan ?");
if(index==3) break; 
}


//]]>
</script>
</body>

【问题讨论】:

  • 尝试在index++之前添加一个console.log。
  • 那个拼写错误(在对confirm() 的调用中使用“reultaat”而不是“resultaat”)会导致错误记录到控制台。

标签: javascript loops while-loop


【解决方案1】:

做这些修改:

//...
var resultaat = toets(index); // Add var before variable
doorgaan = confirm(resultaat+" Wil je doorgaan ?"); // reultaat --> resultaat 
if(index==3) break; 
//...

【讨论】:

  • 认为我的 Firefox 和 html 合谋对付我
  • 加载 HTML 后按 Ctrl+Shift+J 并检查最后的错误。我敢打赌你可以调试很多问题。 :-)
  • 非常感谢大家,你们是我遇到过的最好的事情哈哈哈!!!!
【解决方案2】:

除了一个拼写错误外,您的代码没有问题。应该是

resultaat = toets(index);
doorgaan = confirm(resultaat+" Wil je doorgaan ?");

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 2014-07-13
    • 2022-10-31
    • 2021-09-15
    • 2019-01-11
    • 1970-01-01
    相关资源
    最近更新 更多