數字猜謎

SHIH, PO-WEI
Ken Ken Shih
Published in
Feb 6, 2022

一、設置基本0~99數字

let answer = Math.floor(Math.random() * 100);  
let n1 = 0;
let n2 = 99;

二、架設while迴圈

while (true) {     
let guess = prompt("你猜哪個數字:( " + n1 + " 到 " + n2 + " )");
}

三、設置0–99的數字變化

if (guess == answer) {         
alert("爆炸了!!");
alert("你被炸死了!");
break;
} else if (guess <= answer) {
n1 = guess;
} else if (guess > answer) {
n2 = guess;
}

四、避開0–99以外的數字

if (guess < n1 || guess > n2) {         
alert("請輸入正確數字");
continue;
}

五、全部code

--

--