1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <title>Document</title> 7 </head> 8 <body> 9 <script> 10 var cNum = Math.ceil(Math.random() * 100) 11 var count = 0 12 do { 13 var num = prompt('請輸入一個1-100的數字') 14 count++ 15 if (num > cNum) { 16 alert('猜大了') 17 } else if (num < cNum) { 18 alert('猜小了') 19 } else { 20 alert('恭喜你猜對了,以共猜了' + count + '次') 21 } 22 } while (num != cNum) 23 </script> 24 </body> 25 </html>