document.addEventListener('DOMContentLoaded', () => { const problemDisplay = document.getElementById('multiplication-problem'); const nextButton = document.getElementById('next-button'); let allMultiplications = []; // すべての九九の組み合わせを格納 let displayedMultiplications = new Set(); // 表示済みの九九を格納(重複防止のためSetを使用) // すべての九九の組み合わせを生成 (1x1 から 9x9 まで) function generateAllMultiplications() { for (let i = 1; i <= 9; i++) { for (let j = 1; j <= 9; j++) { allMultiplications.push({ factor1: i, factor2: j, problem: `${i} × ${j} = ${i * j}` }); } } // 九九のリストをシャッフル shuffleArray(allMultiplications); } // 配列をシャッフルするFisher-Yatesアルゴリズム function shuffleArray(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; // 要素を交換 } } // 次の九九を表示 function displayNextMultiplication() { if (displayedMultiplications.size === allMultiplications.length) { problemDisplay.textContent = 'すべての九九を表示しました!'; nextButton.disabled = true; return; } let nextProblem = null; for (const problem of allMultiplications) { // まだ表示されていない九九を見つける if (!displayedMultiplications.has(problem.problem)) { nextProblem = problem; break; } } if (nextProblem) { problemDisplay.textContent = nextProblem.problem; displayedMultiplications.add(nextProblem.problem); } else { // ここに到達することは通常ないはずですが、念のため problemDisplay.textContent = 'エラー:次の九九が見つかりません。'; nextButton.disabled = true; } } // 初期化処理 generateAllMultiplications(); displayNextMultiplication(); // 最初の九九を表示 // 「次へ」ボタンのクリックイベントリスナー nextButton.addEventListener('click', displayNextMultiplication); });