循環應該把每本書的價格,將其添加到總,然後把平均在頁面上的每本書,直到用戶在「N」進入爲什麼我的JavaScript循環不工作?
<script type="text/javascript">
var ct = 1;
var yesORno = "Y";
while (yesORno = "Y"){
book = prompt("What is the price of book #" + ct, 0);
total = parseInt(book) + total;
ans = total/ct;
document.write("<p>With book #" + ct +" The average is " + ans + "</p>");
ct = ct + 1;
yesORno = prompt("Would you like to continue? (Y/N)", "")
}
</script>
這是一個無限循環。 –
'whileORno =「Y」){'should be'while(yesORno ===「Y」){'使用http://jshint.com/來查找代碼中的常見錯誤。 –
(可選)您還應該檢查「y」 –