[英]I am trying to write a while loop in javascript. My console.log does not print the requested message
var ask = prompt('Are we there yet???'); while (ask != 'y') { if (ask[0] === 'y') { // For some unknown reason to me my solution will not print the message. console.log('Yea, we made it!!!'); } else { var ask = prompt('Are we there yet???'); }; }
您的代碼正在將 while 循環內的變量設置為提示的輸出,這就是循環無法訪問它的原因。
為了實現你的目標,你需要這樣的東西:
while (prompt('Are we there yet???') !== 'y') {}
console.log('Yea, we made it!!!');
基本上,代碼進入一個無限循環,要求用戶在繼續代碼之前輸入y
,在這種情況下,將消息記錄到控制台。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.