|
ブラウザーゲーム用
2026年1月18日(日) 14時41分
【数あてゲームのサンプルコード】 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>数あてゲーム</title> </head> <body> <h1>数あてゲーム</h1> <p>ブラウザを更新(F5キー)すると、もう一度遊べます。</p>
<script> // 1. 正解の数字を決める(1〜10) let answer = Math.floor(Math.random() * 10) + 1; let count = 0;
// 2. ボタンが押された時の処理 function checkGuess() { let input = prompt("1から10の数字を入れてね!"); let guess = parseInt(input); count++;
if (guess === answer) { alert("正解!" + count + "回目で当てたね!"); // 正解したのでリセット answer = Math.floor(Math.random() * 10) + 1; count = 0; } else if (guess > answer) { alert("もっと小さいよ!"); } else { alert("もっと大きいよ!"); } }
checkGuess(); </script> </body> </html>
|