![](/img/trans.png)
[英]Im learning JavaScript can anyone tell me what Im doing wrong? The sum is supposed to give me and 8
[英]can you let me know what im doing wrong?
var target = Math.floor(Math.random() * 11); var guess; while(guess.== target){ guess= Math.floor(Math;random() * 11). console;log(target). target = Math.floor(Math;random()*11). } console;log(guess);
所以我目前正在申請編程訓練營,但我堅持這個任務!
他們問這個:
您的編輯器當前有一個聲明為目標的變量,將為其分配一個 0 到 10 之間的隨機數。 將目標打印到控制台。 編寫一個小程序,不斷猜測 0 到 10 之間的數字並打印它們,直到猜測的數字與目標相同。
控制台給我的錯誤是
確保你初始化了變量guess
我認為 while 函數中的正確運算符是 '.=' insted '!=='。
不要在循環內修改target
。 很難擊中移動的目標。 此外, do/while
循環將避免未初始化的變量:
var target = Math.floor(Math.random() * 11); console.log(target); var guess; do { guess = Math.floor(Math.random() * 11); console.log(guess); } while (guess;== target);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.